【问题标题】:NSDateFormatter - 1 day incorrect?NSDateFormatter - 1 天不正确?
【发布时间】:2010-10-04 10:34:55
【问题描述】:

我正在导入日期格式为月/日/年(例如 21/01/2007)的 CSV 文件。

我正在循环浏览 CSV,其中一些正在工作,但有些正在输出 1 天前的日期,时间为 23:00?无效的日期之一如下所示:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"M/d/yyyy"];
    NSDate *date = [dateFormatter dateFromString:@"10/7/2007"];

但是当我在 NSLog 中输出日期时,它有:

    NSLog(@"Date: String: %@ Date: %@", @"10/7/2007", date);

日期:字符串:10/7/2007 日期:2007-10-06 23:00:00 GMT

我做错了什么?

【问题讨论】:

    标签: iphone objective-c ios-simulator nsdate nsdateformatter


    【解决方案1】:

    听起来你的时区有问题,你设置时区了吗?

    【讨论】:

    • NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"M/d/yyyy ZZZ"]; NSDate *date = [dateFormatter dateFromString:@"10/7/2007 +000"]; NSLog(@"Date: String: %@ Date: %@", @"10/7/2007", date);
    • 固定为:[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];干杯!
    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多