NSDateFormatter的区别 [英] NSDateFormatter difference

查看:79
本文介绍了NSDateFormatter的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种日期格式之间有什么区别.第一个给出实际时间,第二个给出时间购买加上时区偏移值.

What is the difference between these two Date Formats. First one give actual time but second on give time buy adding time zone offset value.

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateConverted = [dateFormatter dateFromString:@"2013-12-02T12:15:43.182Z"];
NSLog(@"Date: %@",dateConverted); // 

日期: 2013-12-02 12:15:43 +0000

NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter1 setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];
NSDate *dateConverted1 = [dateFormatter1 dateFromString:@"2013-12-02T12:15:43.182Z"];
NSLog(@"%@",dateConverted1);

日期: 2013-12-02 06:45:43 +0000

推荐答案

第二种格式的问题是所有不必要的引号,尤其是Z周围的引号.引用Z表示Z被视为文字字符,而不是时区格式说明符.

The problem with the 2nd format is all of the needless quotes, especially around the Z. By quoting the Z this means the Z is treated as a literal character and not the timezone format specifier.

去掉Z周围的引号,它们都将得到相同的结果.

Get rid of the quotes around the Z and both will give the same result.

这篇关于NSDateFormatter的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆