如何从字符串读取NSDate? [英] How to read a NSDate in from a string?
问题描述
我有字符串的日期,并希望将它们解析为NSDate对象。有办法吗?我看过NSDate和NSScanner,没有看到任何从字符串中读取的内容。
I have strings with dates, and want to parse these into NSDate objects. Is there a way to do that? I've looked at NSDate and NSScanner, and haven't seen anything that would read it in from a string.
推荐答案
可可sdk,通常:如果你想要一个日期和一个字符串,查看Date类的接受一个字符串的create方法。
In the cocoa sdk, it's usually: If you want a date and have a string, look in the "Date" class for a create method that accepts a string.
[ NSDateFormatter dateFromString:myString]
[NSDateFormatter dateFromString:myString]
dateFromString:
dateFromString:
返回一个给定字符串的
的日期表示形式,使用
接受者的当前设置。
Returns a date representation of a given string interpreted using the receiver’s current settings.
(NSDate *)dateFromString :( NSString *)string
(NSDate *)dateFromString:(NSString *)string
所以如果你想要一个字符串并有一个日期,你需要一个NSString静态函数stringFromDate m猜测它存在,但没有看到它)。我会承认,NSDateFormatter不会打击我作为第一个地方,我会寻找这个功能,虽然。
So if you want a string and have a date, you look for an NSString Static function called stringFromDate (i'm guessing it exists but didn't look it up). I will concede that NSDateFormatter doesn't strike me as the first place i'd look for this function though.
这篇关于如何从字符串读取NSDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!