How-To: Convert a string to NSDate
Dec.05, 2008 in
Development, How To, iPhone Development
In Objective-C, you commonly nead to convert a string to an NSDate object. A simple way to do this is using the NSDateFormatter object. It provides the dateFromString method which converts a string into an NSDate object. You do, however, need to tell NSDateFormatter the format the date will be in. See below example:
NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"]; NSDate *myDate = [df dateFromString: myDateAsAStringValue];
NSDateFormatter uses the Unicode Locale Data Markup Language to determine the date format. You can view specific formatting options on the Unicode website.
Tags: iPhone Development, NSDate

March 12th, 2009 at 7:09 pm
How-To: Convert a string to NSDate…
You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…
September 27th, 2009 at 6:17 pm
Awesome, Thanks!
May 28th, 2010 at 1:40 am
First line should be autorelease
July 20th, 2010 at 6:40 am
Great! Thanks!