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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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]
dateFromString:
Returns a date representation of a given string interpreted using the receiver’s current settings.
(NSDate *)dateFromString:(NSString *)string
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.
回答2:
Try NSDateFormatter