NSDateFormatter, am I doing something wrong or is this a bug?

后端 未结 8 1015
-上瘾入骨i
-上瘾入骨i 2020-11-30 06:18

I\'m trying to print out the date in a certain format:

NSDate *today = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]         


        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 07:06

    Setting locale on date formatter to en_US fixes the problem for me:

        NSDateFormatter * f = [[NSDateFormatter alloc] init];
        [f setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
        f.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
        f.calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
        f.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
    

    I'm not sure if adding the calendar is also needed, but this works well.

提交回复
热议问题