iOS NSDate Comparison works differently when the 24-Hour Time in date settings toggles between ON and OFF?

前端 未结 2 1983
清酒与你
清酒与你 2020-12-06 14:34

Team, I am comparing the date which is formed from string using NSDateFormatter with the iOS system date. The below statement returns true when the system date time settings

相关标签:
2条回答
  • 2020-12-06 15:03

    23 May 2013 15:37:00 is a 24 hour format string.So the correct date is obtained using the 24 hour format date formatter .Thats it

    So use

    [fmt setDateFormat:@"dd MMM yyyy HH:mm:ss"];
    
    0 讨论(0)
  • 2020-12-06 15:15

    See Apple's Technical Q&A 1480.

    You need to set the date formatter's locale to the special locale of en_US_POSIX. You also need to specify a 24-hour hour format - HH, not hh.

    -(NSDate *)getDateFromString:(NSString *)dateString{
        NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
        NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
        [fmt setLocale:posix];
        [fmt setDateFormat:@"dd MMM yyyy HH:mm:ss"];
    
        return [fmt dateFromString:dateString];
    }
    
    0 讨论(0)
提交回复
热议问题