NSDateFormatter dateFromString and iPhone in 24 Hour Format Confusion

前端 未结 5 1629
小蘑菇
小蘑菇 2020-12-03 05:04

I\'m having a problem. I get incoming time strings in 12-hour format, and I\'m turning them into NSDate objects. When the iPhone is in 12 hour format, no problem. But when i

5条回答
  •  情深已故
    2020-12-03 05:24

    I believe the @"h:mm a" should be @"HH:mm a".

    If you use the pre-build dateformatter in cocoa, everything will be taken care of for you.

    NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [timeFormatter setTimeStyle:NSDateFormatterShortStyle];
    [timeFormatter setDateStyle:NSDateFormatterNoStyle];
    

    NSDateFormatterShortStyle and NSDateFormatterNoStyle comes in different varieties. Using those will make sure you respect the settings the user has selected for dates and times.

    The 12-14 hour clock conversion is taken care of by the SDK, if you have a model or some value object for storing your dates try to keep them as NSDate. This way you can format them only when you need to display them. Saving dates as strings could open a world of trouble when you maybe parse them from xml where the GMT is specified separately or try to add and subtract NSTimeIntervals.

提交回复
热议问题