NSDateFormatter in 12-hour mode

我的梦境 提交于 2019-12-03 09:35:31

Try to set the locale in this way :

NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
df.locale = twelveHourLocale;

To force instead to 24 hour, you can use :

NSLocale *twentyFour = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];

In your NSDateFormatter "yyyy-MM-dd'T'HH:mm:ss.SSSZZZ" HH stands for 24 hour and hh stands for 12 hour

12-hour mode to 24 hour mode:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];
NSDate *amPmDate = [formatter dateFromString:amPmHourString];
[formatter setDateFormat:@"HH:mm"];
NSString *24HourString = [formatter stringFromDate:amPmDate]];

For 24-hour mode to 12 hour mode just do the opposite

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[[NSLocale alloc] 
                initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];  
[dateFormat setLocale:locale];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!