iOS 24 Hour date format

If you are using NSDateFormatter to send dates back to a server be VERY careful.  Specifying a date format string as follows…

@”yyyy-MM-dd’T'HH:mm:ss”

and the time for example is currently  09/10/2013  14:00:00, if the user has set their clock to be a 12-hour clock, instead of the default 24-hour clock. You will get a string back of…

2013-10-09T02:00:00 PM

The users preferences overwrite what you are asking for!!

Read the following documentation - https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html

The following sample code from that article will always return a 24-hour clock value regardless of user preferences

<br /><br />NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];<br /><br />NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];<br /><br />[rfc3339DateFormatter setLocale:enUSPOSIXLocale];<br /><br />[rfc3339DateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];<br /><br />[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];<br /><br />NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];<br /><br />

Comments are closed.