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

前端 未结 2 1077
离开以前
离开以前 2020-12-31 08:08

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).

2条回答
  •  不知归路
    2020-12-31 08:50

    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
    

提交回复
热议问题