iPhone NSDateFormatter时区转换 [英] iPhone NSDateFormatter Timezone Conversion

查看:92
本文介绍了iPhone NSDateFormatter时区转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个格式化程序,将显示的日期格式转换为NSDate对象:

I am trying to create a formatter that will convert the date format shown to an NSDate object:

NSString *dateStr = @"2010-06-21T19:00:00-05:00";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [dateFormat dateFromString:dateStr];  

问题是时区-05:00,使用上述格式无法正确解析。有什么建议?

The issue is the timezone -05:00, which is not parsed properly with the format above. Any suggestions?

推荐答案

老实说,你只需要在运行它之前更改源数据(删除冒号)格式化。您的原始日期字符串是非标准的,并且没有任何时区格式字符串可以在其上正常工作。

Honestly, you'll just have to change the source data (removing the colon) before running it through the formatter. Your original date string is non-standard and none of the time zone format strings will work properly on it.

您可以看到有效输入

You can see the valid inputs on unicode.org.

ZZZ例如-0500

ZZZ e.g. "-0500"

ZZZZ例如GMT-05:00

ZZZZ e.g. "GMT-05:00"

-05:00没有任何内容

Nothing for "-05:00"

这篇关于iPhone NSDateFormatter时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆