to convert a NSString
into a NSDate
, please, use the NSDateFormatter
.
NSString *_dateString = @"August 11, 2012, 10:17 AM";
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:@"MMMM dd, yyyy, HH:mm a"];
NSDate *_date = [_dateFormatter dateFromString:_dateString];
NSLog(@"raw date : %@", _date);
NSLog(@"formatted date : %@", [_dateFormatter stringFromDate:_date]);
the output NSDate
will contains the date:
raw date : 2012-08-10 23:17:00 +0000
formatted date : August 11, 2012, 11:17 AM
you can format the NSDate
as you'd like for a different output, it is up to up now, but you can work with the NSDate
object which contains the date.
NOTE: we don't know the timezone from the input date when we are converting it, so the NSDate
using the defaults for it. if you set the current timezone you will get the correct date after conversion.