Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSDate to long format string and back again?

How would I convert an NSDate into a string like this:

Monday, November 22, 2010

.. and then back again into an NSDate?

like image 357
cannyboy Avatar asked Nov 22 '10 22:11

cannyboy


1 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 details I'm not covering, such as the Locale issues and whatnot, but this should get you going

like image 90
Miguel Andrés Yáñez Avatar answered Sep 27 '22 23:09

Miguel Andrés Yáñez