【发布时间】:2014-01-18 06:07:50
【问题描述】:
我有这段代码:
NSDateFormatter* df = [[NSDateFormatter alloc] init];
df.locale = [NSLocale currentLocale];
df.timeZone = [NSTimeZone localTimeZone];
df.timeStyle = NSDateFormatterShortStyle;
df.dateStyle = NSDateFormatterShortStyle;
df.locale = [NSLocale currentLocale];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *todaysDate = @"31/12/2013";
NSDate* today = [df dateFromString:todaysDate];
NSLog(@"comparing todaysDate: %@ with today: %@",todaysDate,today);
当我检查我的控制台时,我看到 todaysDate 是 31/12/2013 而今天是 2013-12-30 21:00:00 +0000 我不明白为什么它会在过去 1 天...帮帮我?我试图基本上根据日期过滤掉字典中的一些项目。此处不包括过滤部分,因为已确定 dateFromString 没有完成其工作。
【问题讨论】:
-
检查您的设备设置->常规->日期和时间->设置->常规->国际->区域格式的时区和设备
-
是的,你的时间设置有问题。在在线 Objective C 编译器中检查代码是完美的
-
"31/12/2013" 在您的时区中与 "2013-12-30 21:00:00 +0000" 相同(以 UTC 报告时间)。 - 这个问题经常被问及回答,见上面的“可能重复”或谷歌“NSDate 错误”。
标签: ios objective-c