NSDateFormatter dateFromString and iPhone in 24 Hour Format Confusion

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

提交回复
热议问题