Convert string to NSDate

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.

Comments

Popular posts from this blog

Integrating ZXing QR Code reader in iPhone / iOS applications

Multiple line of text in UIPickerView

Connect Samsung devices to Kies on Mac