Reputation: 131
I am storing dates as strings in the format: 2018-07-10 21:00:29 +0000
I created a formatter to convert the strings back into dates:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
[formatter setLocale:[NSLocale currentLocale]];
NSDate *date = [formatter dateFromString:[chore getDate]];
However, the date that is returned is always nil. Does anyone know what I'm doing wrong?
Upvotes: 1
Views: 60
Reputation: 156
You set the wrong format in the dateformat. So it will always return the nil value.
The following method can return the date in the NSDate format.
-(NSDate *)convertDate :(NSString *)date1 //the argument date1 is the string date you used
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z"; // the format which you used in the string date
[formatter setLocale:[NSLocale currentLocale]]; //This for set the Locale .It is not compulsory.
NSDate *date = [formatter dateFromString:date1];
return date;
}
Upvotes: 4
Reputation: 2084
Refer to date formatter date formats here:
You need to update your date format to something like:
"yyyy-MM-dd HH:mm:ss Z"
Upvotes: 1