NSDateformatter setDateFormat according to currentLocale
Asked Answered
A

6

12

I'm going mad with, probably, a stupid problem.

I have 3 strings: year, month and day. I need to have a date in the right format based on currentLocale, so i.e. if currentLocale localeIdentifier is en_US my dateFormat should be: MMM/dd/yyyy

if it's fr_FR the dateFormat should be dd/MMM/yyyy

I don't think the only way to do this is to get currentLocale localeIdentifier and start with a bunch of if then.

Thanks in advance.

Max

Abiosis answered 9/5, 2011 at 15:4 Comment(0)
D
7

Look at NSDateComponents to create an NSDate, then use NSDateFormatter to format it. NSDateFormatter uses the current locale to format dates, based on the format style (e.g.
NSDateFormatterMediumStyle).

Dungaree answered 9/5, 2011 at 15:9 Comment(1)
Thank you much. NSDateComponents helped.Abiosis
B
34

If I understand your question, you want to set your NSDateFormatter to the locale of the user's device. For that you can just do something like this:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
Beatify answered 9/5, 2011 at 15:11 Comment(0)
D
7

Look at NSDateComponents to create an NSDate, then use NSDateFormatter to format it. NSDateFormatter uses the current locale to format dates, based on the format style (e.g.
NSDateFormatterMediumStyle).

Dungaree answered 9/5, 2011 at 15:9 Comment(1)
Thank you much. NSDateComponents helped.Abiosis
M
4

In Swift 3:

let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
formatter.locale = Locale.current
let date = Date()
let dateString = formatter.string(from: date)
print(dateString)
Magistrate answered 28/7, 2015 at 9:25 Comment(0)
E
2
-(NSString *) stringFromDate:(NSDate *) date{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

    [dateFormatter setLocale:[NSLocale currentLocale]];

    NSString *dateString = [dateFormatter stringFromDate:date];

    [dateFormatter release];

    return dateString;
}

-(NSDate *) dateFromString:(NSString *) dateInString{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

    [dateFormatter setLocale:[NSLocale currentLocale]];

    NSDate *dateFromString = [dateFormatter dateFromString:dateInString];

    [dateFormatter release];

    return dateFromString;
}

I hope this helps.

Executant answered 9/5, 2011 at 15:12 Comment(0)
C
1

check out this link to get understand of how NSDateFormatter based on http://www.codingexplorer.com/swiftly-getting-human-readable-date-nsdateformatter/

Couple answered 23/9, 2016 at 5:0 Comment(0)
O
0

In case you want to have more flexibility about the format than the formats usable with setDateStyle, have a look at my answer to a similar question: https://mcmap.net/q/420417/-nsdate-in-full-style-but-without-year

Ousley answered 26/11, 2013 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.