skaak
skaak

Reputation: 3018

Localised date without year

In iOS I can format a date for the system locale. Is it possible to format a date for the system locale but excluding the year, e.g. say '7/17' for Europa and '17/7' for US?

EDIT

I'm doing this at present

        df = [[NSDateFormatter alloc] init];

        df.locale = NSLocale.systemLocale;
        [df setLocalizedDateFormatFromTemplate:@"MMMMd"];

but wondering if there is an equivalent to getting e.g. the short- or long formatted date BUT without the year?

Upvotes: 0

Views: 67

Answers (1)

Ol Sen
Ol Sen

Reputation: 3368

recognize the difference between NSLocale.systemLocale and NSLocale.currentLocale.
First one is general system format, last one is following the users settings. so your date formatter NSDateFormatter *df = [[NSDateFormatter alloc] init]; is sensitive to what format you refer to.

the following should output the short formatted day and month = Jul 17.

df.locale = NSLocale.currentLocale;
[df setLocalizedDateFormatFromTemplate:@"dMMM"];
NSLog(@"%@",[df stringFromDate:[NSDate date]]);

this should output the long formatted day and month = July 17.
dd means also day with leading zero i.e. 07 for the 7th day of month.

df.locale = NSLocale.currentLocale;
[df setLocalizedDateFormatFromTemplate:@"ddMMMM"];
NSLog(@"%@",[df stringFromDate:[NSDate date]]);

but systemLocale is different. It will output = 07-17.

df.locale = NSLocale.systemLocale;
[df setLocalizedDateFormatFromTemplate:@"ddMM"];
NSLog(@"%@",[df stringFromDate:[NSDate date]]);

and even more fun and maybe what you are looking for. output = 07/17.

df.locale = NSLocale.currentLocale;
[df setLocalizedDateFormatFromTemplate:@"ddMM"];
//[df setLocalizedDateFormatFromTemplate:@"MMdd"]; // sorting does'nt matter
NSLog(@"%@",[df stringFromDate:[NSDate date]]);

Upvotes: 1

Related Questions