gpt4 book ai didi

ios - 如何从时间字符串获取时区

转载 作者:行者123 更新时间:2023-11-29 02:22:49 27 4
gpt4 key购买 nike

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

最佳答案

我建议使用库来格式化日期数据格式。我可以推荐 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

关于ios - 如何从时间字符串获取时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27926486/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com