Hours in NSDate every time in 24-hour style?

前端 未结 5 1622
小鲜肉
小鲜肉 2021-02-05 16:56

I have this date formatter:

 NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
 [timeFormatter setDateFormat:@\"HH:mm\"];

If I u

5条回答
  •  别那么骄傲
    2021-02-05 17:21

    If you want to force it to 12-hour or 24-hour mode, regardless of the user's 24/12 hour mode setting, you should set the locale of the date formatter to en_US_POSIX (for 12-hour), or, say, en_GB for the 24-hour mode.

    That is,

    NSLocale* formatterLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease];
    [timeFormatter setLocale:formatterLocale];
    

    Some more on that here:

    http://developer.apple.com/iphone/library/qa/qa2010/qa1480.html

提交回复
热议问题