如何在iOS上将NSString转换为NSDate ?

时间:2022-09-27 23:15:22

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对象转换为NSDate,但是尝试向Apple提交一个更新,但是它被拒绝了。在iOS中,还有什么方法可以做到这一点?

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

Thanks in advance!

提前谢谢!

1 个解决方案

#1


9  

Use an NSDateFormatter. Brief example follows:

使用一个NSDateFormatter。简单的例子:

...
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];
...

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

注意,根据Unicode技术标准#35,NSDateFormatter本身不支持单字母时区标识符(即zulu时间的Z)。

#1


9  

Use an NSDateFormatter. Brief example follows:

使用一个NSDateFormatter。简单的例子:

...
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];
...

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

注意,根据Unicode技术标准#35,NSDateFormatter本身不支持单字母时区标识符(即zulu时间的Z)。