【问题标题】:utc and local date time conversionUTC 和本地日期时间转换
【发布时间】:2012-11-28 10:57:33
【问题描述】:

服务器给出 UTC 格式的日期和时间。

2012-11-28T10:32:15+01:00

UTC 将被格式化为:2012-11-28 10:32:15 当前实际的本地日期和时间是2012-11-28 11:32:15

我想将 utc 格式保存在数据库中,稍后在 UI 中显示之前将其转换为用户的本地日期和时间。我写了以下代码。但时间偏移是错误的。即使时间偏移量为 + 1 小时,它也会在 UTC 时间上增加 2 小时而不是 1 小时。

NSDateFormatter *utc = [[NSDateFormatter alloc] init];
[utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[utc setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dtUTC = [utc dateFromString:@"2012-11-28 10:32:15"];

double i =[[NSTimeZone systemTimeZone] secondsFromGMTForDate:[NSDate date ]];

NSDateFormatter *dfLocal = [[NSDateFormatter alloc] init];
[dfLocal setDateFormat:@"yy-MM-dd HH:mm:ss"];
[dfLocal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:i]];
NSDate *localDateTime= [dtUTC dateByAddingTimeInterval:i];
NSString *time =[dfLocal stringFromDate:localDateTime];
NSLog(@"original UTC %@  now local: %@", dtUTC, time);

还有日志:

time offset : 1.000000
original UTC 2012-11-28 10:32:15 +0000  now local: 12-11-28 12:32:15

它应该是2012-11-28 11:32:15,但给出2012-11-28 12:32:15 我做错了什么?

【问题讨论】:

标签: iphone objective-c ios


【解决方案1】:

在发布问题后现在就可以了。我找到了解决方案。太神奇了!

NSDateFormatter *utc = [[NSDateFormatter alloc] init];
[utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[utc setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dtUTC = [utc dateFromString:@"2012-11-28 10:32:15"];

NSDateFormatter *dfLocal = [[NSDateFormatter alloc] init];
[dfLocal setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dfLocal setTimeZone:[NSTimeZone localTimeZone]];

NSString *time =[dfLocal stringFromDate:dtUTC];
NSLog(@"original UTC %@  now local: %@", dtUTC, time);

还有日志:

time offset : 1.000000
original UTC 2012-11-28 10:32:15 +0000  now local: **2012-11-28 11:32:15**

感谢大家帮忙

【讨论】:

    【解决方案2】:

    改变这一行

    [dfLocal setDateFormat:@"yy-MM-dd HH:mm:ss"];
    

    替换成

     [dfLocal setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    

    编码愉快。!!!!!!

    【讨论】:

    • 这不起作用,因为它与添加时间偏移无关。我之前试过:-)
    【解决方案3】:

    也试试这个代码..

    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    
    NSDate *dtUTC = [utc dateFromString:@"2012-11-28 10:32:15"];
    
    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;
    
    NSDateFormatter *dfLocal = [[NSDateFormatter alloc] init];
    [dfLocal setDateFormat:@"yy-MM-dd HH:mm:ss"];
    NSDate* localDateTime = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:dtUTC] autorelease];
    NSString *time =[dfLocal stringFromDate:localDateTime];
    NSLog(@"original UTC %@  now local: %@", dtUTC, time);
    

    希望对你有帮助……

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多