Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if locale's date format is Month/Day or Day/Month?

In my iPhone app, I'd like to be able to determine if the user's locale's date format is Month/Day (i.e. 1/5 for January fifth) or Day/Month (i.e. 5/1 for January fifth). I have a custom NSDateFormatter which does not use one of the basic formats such as NSDateFormatterShortStyle (11/23/37).

In an ideal world, I want to use NSDateFormatterShortStyle, but just not display the year (only month & day#). What's the best way to accomplish this?

like image 237
Jason Avatar asked Feb 27 '11 19:02

Jason


People also ask

How are Mexican dates formatted?

For Mexico, the day comes before the month, everything is lowercase and the article "de" ia used. In Japan, the day of the week is not displayed and the translations for "year", "month" and "day" act like numeric separators. In the US, the full weekday name is followed by the month name and day number, then the year.

What date format is dd mm yyyy?

The United States is one of the few countries that use “mm-dd-yyyy” as their date format–which is very very unique! The day is written first and the year last in most countries (dd-mm-yyyy) and some nations, such as Iran, Korea, and China, write the year first and the day last (yyyy-mm-dd).

Which countries use date format mm-dd-yyyy?

Despite the variety of date formats used around world, the US is the only country to insist on using mm-dd-yyyy.


1 Answers

You want to use NSDateFormatter's +dateFormatFromTemplate:options:locale:

Here is some Apple sample code:

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];

NSString *dateFormat;
NSString *dateComponents = @"yMMMMd";

dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];
NSLog(@"Date format for %@: %@",
    [usLocale displayNameForKey:NSLocaleIdentifier value:[usLocale localeIdentifier]], dateFormat);

dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:gbLocale];
NSLog(@"Date format for %@: %@",
    [gbLocale displayNameForKey:NSLocaleIdentifier value:[gbLocale localeIdentifier]], dateFormat);

// Output:
// Date format for English (United States): MMMM d, y
// Date format for English (United Kingdom): d MMMM y
like image 158
sbooth Avatar answered Sep 21 '22 14:09

sbooth