Objective C 日期转换
Objective C date Conversion
对于我来说,我似乎无法将此 nsstring 转换为带有 dateformatter 的 nsdate,它来自 xml 这种格式的提要:2016-06-03T19:00:00+00:00
我正在使用此代码,但日期返回 null。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'ZZZZ'";
NSTimeZone *timeZoney = [NSTimeZone timeZoneWithName:@"UTC"];
dateFormatter.timeZone = timeZoney;
NSLog(@"passed: %@",theDate);
NSDate *convertedDate = [dateFormatter dateFromString:theDate];
NSLog(@"Date is %@",convertedDate);
我一直在使用这个网站 http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns 来尝试弄清楚它,但我似乎无法让它工作。任何帮助将不胜感激。
您不需要 ZZZZ
周围的那些单引号 -- 它们表示包含的字母应该与您键入的完全一样,就像 'T'
一样。删除它们并进行转换:
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZ";
对于我来说,我似乎无法将此 nsstring 转换为带有 dateformatter 的 nsdate,它来自 xml 这种格式的提要:2016-06-03T19:00:00+00:00
我正在使用此代码,但日期返回 null。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'ZZZZ'";
NSTimeZone *timeZoney = [NSTimeZone timeZoneWithName:@"UTC"];
dateFormatter.timeZone = timeZoney;
NSLog(@"passed: %@",theDate);
NSDate *convertedDate = [dateFormatter dateFromString:theDate];
NSLog(@"Date is %@",convertedDate);
我一直在使用这个网站 http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns 来尝试弄清楚它,但我似乎无法让它工作。任何帮助将不胜感激。
您不需要 ZZZZ
周围的那些单引号 -- 它们表示包含的字母应该与您键入的完全一样,就像 'T'
一样。删除它们并进行转换:
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZ";