0

这是我的代码

NSString * dateStr = @"7/25/2013 12:00:00 AM";

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];

NSLog(@"dateStr : %@ , date : %@",dateStr,date);

但我的日期将比实际日期晚一天。查看此日志

dateStr : 7/25/2013 12:00:00 AM , date : 2013-07-24 18:30:00 +0000

我究竟做错了什么?

4

3 回答 3

1

试试这个。

NSString * dateStr = @"7/25/2013 12:00:00 AM";

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
 [dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:dateStr];

NSLog(@"dateStr : %@ , date : %@",dateStr,date);
于 2013-07-09T12:22:19.293 回答
0

试试这样的东西。这会帮助你

 NSString * dateStr = @"7/25/2013 12:00:00 AM";

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
    NSDate *date = [dateFormat dateFromString:dateStr];
于 2013-07-09T12:23:10.877 回答
0
 NSString * dateStr = @"7/25/2013 12:00:00 AM";
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    [dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
    NSDate *date = [dateFormat dateFromString:dateStr];

    NSLog(@"dateStr : %@ , date : %@",dateStr,date);

设置时区可解决此问题

于 2013-07-09T12:29:48.863 回答