在 iOS 上将 NSString 转换为 NSDate 的正确方法? [英] Proper way to convert NSString to NSDate on iOS?

查看:80
本文介绍了在 iOS 上将 NSString 转换为 NSDate 的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用此方法将常规 NSString 对象转换为 NSDate,但尝试向 Apple 提交更新并被拒绝.在 iOS 中执行此操作的另一种方法是什么?

I've been using this method to convert a regular NSString object to an NSDate, but tried submitting an update to Apple and it was denied. What's another way to do this in iOS?

NSString *date_str = @"2011-08-12T12:20:00Z";
NSDate *the_date = [NSDate dateWithNaturalLanguageString:date_str locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];

提前致谢!

推荐答案

使用 NSDateFormatter.简要示例如下:

Use an NSDateFormatter. Brief example follows:

...
NSString* dateString = "2011-08-12T12:20:00Z";
NSDateFormatter* fmt = [NSDateFormatter new];
[fmt setDateFormat:"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate* date = [fmt dateFromString:dateString];
[fmt release];
...

请注意,根据 Unicode 技术标准 #35NSDateFormatter 本身不支持单字母时区标识符(即 Z 代表祖鲁时间).

Note that as per Unicode Technical Standard #35, NSDateFormatter doesn't natively support single-letter time-zone identifiers (i.e. Z for zulu time).

这篇关于在 iOS 上将 NSString 转换为 NSDate 的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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