NSDateFormatter 按年和月格式化日期
我尝试将 NSDateFormatter 设置为年份,如下所示:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy"];
NSLog(@"**> %@",[dateFormatter dateFromString:@"2012"]);
[dateFormatter release];
但它打印: **> 2011-12-31 22:00:00 +0000
也许有人可以帮助我解决这个问题
I tried to set NSDateFormatter for year, like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy"];
NSLog(@"**> %@",[dateFormatter dateFromString:@"2012"]);
[dateFormatter release];
But it prints: **> 2011-12-31 22:00:00 +0000
Maybe someone could help me about this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与 UTC (GMT) 和您的时区(您可能处于 GMT+2)之间的时区差异有关。 NSDate 默认采用 UTC。这就是为什么 [NSDate date] 可能会偏离当地时间。
以下代码应输出 Jan 1, 2012:
或
This has to do with the timezone difference between UTC (GMT) and your time zone (you're probably in GMT+2). NSDate is in UTC by default. That's why [NSDate date] can deviate from the local time.
The following code should output Jan 1, 2012:
or
希望您来自 +02:00 时区。由于 NSDate 始终给出 GMT 时区的时间,因此它不会打印
2012-01-01 00:00:00 +0000
,而是打印上述值,即 GMT 时区的时间。编辑:
Hope you are from +02:00 timezone. As NSDate always gives the time in GMT timezone, instead of printing
2012-01-01 00:00:00 +0000
, it is printing the above value which is the time in GMT timezone.Edit: