就这么简单
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy"];
NSLog(@"Current date in event %@ %@",currentDate,[df stringFromDate:currentDate]);
但是在日志中
Current date in event 2013-10-01 22:00:00 +0000 02/10/2013
为什么?
最佳答案
当您记录一个 NSDate
时,它总是以 UTC 时间打印。当您使用 stringFromDate:
时,除非您更改日期格式化程序的时区,否则您将获得本地时间的日期。
在这种情况下,您所在的时区比 UTC 早两个小时(假设您在意大利)。
由于 NSDate
对象是 2013 年 10 月 1 日 22:00 UTC,这与 2013 年 10 月 2 日午夜 UTC+2 相同。这就是为什么您会看到显示 10 月 2 日的字符串。
这是另一种看待它的方式。您没有看到 1 天的差异。由于您所在的时区,您实际上看到了 2 小时的差异。从午夜到您所在时间的 0200 时,您会看到似乎有 1 天的差异。
关于ios - NSDateFormatter 日期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19122239/