转换NSDateFormatter时,IOS系统时间不同

时间:2014-03-24 08:42:27

标签: ios objective-c

我正在设置NOW DATETIME,就像这样

 NSDate* now = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone systemTimeZone] secondsFromGMT]];

并使用值名称'now',尝试像这样转换为NSString

    NSDate *date = [object valueForKey:@"dateTime"];// valueForKey:@"dateTime"=now
    NSLog(@"%@",date);
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];


    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"yyyy"];
    NSString *year = [formatter stringFromDate:date];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"MM"];
    NSString *month = [formatter stringFromDate:date];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"dd"];
    NSString *day = [formatter stringFromDate:date];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"HH"];
    NSString *hour = [formatter stringFromDate:date];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"mm"];
    NSString *minute = [formatter stringFromDate:date];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"ss"];
    NSString *second = [formatter stringFromDate:date];

NSLog(@"%@",date);
NSLog(@"%@/%@/%@ %@:%@:%@ ",year,month,day,hour,minute,second);

consol LOG

2014-03-24 17:32:36.034 SecurityGate[7147:60b] 2014-03-24 15:51:29 +0000
2014-03-24 17:32:36.037 SecurityGate[7147:60b] 2014/03/25 00:51:29 

我想做同样的(系统时间是Japan.tokyo) 请帮我。

1 个答案:

答案 0 :(得分:0)

你的错误在这行代码中:

NSDate *date = [object valueForKey:@"dateTime"];// valueForKey:@"dateTime"=now

评论声称它现在是#34;现在"但事实并非如此。您的NSLog输出是:

2014-03-24 17:32:36.034 SecurityGate[7147:60b] 2014-03-24 15:51:29 +0000

注意一开始它有NSLog的时间,可能是你当地的时区(东京?)。

但是印刷的日期完全不同。小时可以通过时区问题来解释,但分钟/秒也是错误的。将它转换为您当地的时区给出了2014-03-25 00:51:29,从我撰写这篇文章开始,将来整整六个小时。

很明显,对象内部的数据不是"现在"你似乎在想。