【问题标题】:How to convert NSString to NSDate using NSDateFormatter?如何使用 NSDateFormatter 将 NSString 转换为 NSDate?
【发布时间】:2012-07-08 22:38:34
【问题描述】:

我从网络服务中获取日期,例如“2012-07-03 07:26:48”。从 web 服务解析后,我已经保存在 NSString 中。在将 NSString 转换为下面代码中使用的 NSDate 时,

    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

    NSString *dateStr = @"2012-07-03 07:26:48";
    NSDate *date = [dateFormatter1 dateFromString:dateStr];
    NSLog(@"Date : %@", date);
   The NSLog value is "2012-07-03 01:56:48 +0000";

NSLog 值为“2012-07-03 01:56:48 +0000”;我不知道为什么时间变了。谁能帮我做到这一点?

【问题讨论】:

  • 在印度……但在美国工作……
  • 你看到哪里有 +0000 了吗?我认为这是说它显示的是 GMT+0000,所以格林威治标准时间就像你在英格兰一样。

标签: iphone ios nsstring nsdate nsdateformatter


【解决方案1】:

默认情况下,将字符串转换为nsdate时,日期以utc格式保存...您可以从日期中获取原始时间作为字符串..如果要更改时区,请使用dateformatter进行更改..否则不要更改

【讨论】:

  • 请不要发明词(如 f.i. "u")
【解决方案2】:

您需要将时区设置为:

theDateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

因为它正在考虑 GMT 时间格式。

【讨论】:

    【解决方案3】:

    你必须设置NSDateFormatter的时区。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多