【问题标题】:How to get timezone from time string如何从时间字符串中获取时区
【发布时间】:2015-03-11 15:58:06
【问题描述】:

NSDate 类不存储有关时区的信息。我必须单独存储NSTimeZone。我找不到任何从字符串中获取时区的方法,例如:"2015-01-09T00:00:00+02:00""2015-01-09T02:00:00Z。你能帮帮我吗?

【问题讨论】:

  • 是的,我明白了"+02:00" Z 相当于 "00:00"
  • 一般来说,你不能。在the timezone tag wiki 中阅读“时区!= 偏移量”。

标签: ios timezone nsdate nstimezone


【解决方案1】:

我建议使用库来格式化日期数据格式。我可以推荐 ISO8601 日期格式化程序 - https://github.com/boredzo/iso-8601-date-formatter 并使用它的 [dateFormatter dateComponentsFromString: 方法来获取 NSDateComponents

编辑:

只是在提到的库中包含示例:

ISO8601DateFormatter *dateFormatter = [[ISO8601DateFormatter alloc] init];
dateFormatter.includeTime = YES;
NSString *testTime = @"2015-01-09T00:00:00+02:00";
NSTimeZone *timeZone;
NSDateComponents *components = [dateFormatter dateComponentsFromString:testTime timeZone:&timeZone];

生产:

Printing description of timeZone:
GMT+0200 (GMT+2) offset 7200

【讨论】:

    猜你喜欢
    • 2021-07-14
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多