Convert NSDate to long format string and vice versa?

How to convert NSDate to a string like this:

Monday, November 22, 2010 

.. and then back to NSDate?

+6
ios objective-c iphone nsdate
source share
2 answers

Use NSDateFormatter

Something like:

 NSDate *date=//...; ; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; [dateFormatter setDateStyle:NSDateFormatterLongStyle]; NSString *string=[dateFormatter stringFromDate:date]; //.... NSDate *newDate=[dateFormatter dateFromString:string]; 

There are more detailed information that I don’t consider, such as problems with Locale and much more, but this should make you go

+6
source share

You need to use the NSDateFormatter class, especially stringFromDate and dateFromString . Links have good examples.

0
source share

All Articles