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

Leave a Reply