NSDateFormatter dateFromString and iPhone in 24 Hour Format Confusion

前端 未结 5 1630
小蘑菇
小蘑菇 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:17

    Not sure if you still need it, but I've had a similar problem which got solved by setting the locale for the date formatter. That is, if you want to force it to 12-hour mode, regardless of the user's 24/12 hour mode setting, you should set the locale to en_US_POSIX.

    0 讨论(0)
  • 2020-12-03 05:21

    The reason for this behaviour is Locale, set the correct Locale

        NSString *strAgendaDate = @"01/17/2012 12:00 AM";
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
        [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
    
        [dateFormatter setDateFormat:AgendaDateFormatForMeeting];
        NSDate *meetingDate = [dateFormatter dateFromString:aStrDate];
        [dateFormatter setDateFormat:AgendaDateRepresentation];
        strAgendaDate = [dateFormatter stringFromDate:meetingDate];
    

    It works for both 24-hour and 12 hour format

    0 讨论(0)
  • 2020-12-03 05:21

    I changed from @"hh:mm:ss" to @"HH:mm:ss" and time style was changed from "1:03 PM" to "13:03". Hope this will help you.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-03 05:32

    Okay, I left a comment, but it squished all the code together, so I'll have to "answer" my question with a comment:

    Thanks. I gave it a whirl with this code:

    NSString *theTime = @"3:19 PM"; 
    NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];       
    [timeFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    [timeFormatter setDateStyle:NSDateFormatterNoStyle]; 
    NSDate *date = [timeFormatter dateFromString:theTime]; 
    NSString *theString = [timeFormatter stringFromDate:date]; 
    

    And date comes up nil. I ran into this earlier when I tried this route, and it's not working. Very frustrating.

    0 讨论(0)
提交回复
热议问题