Every time I try to pass a date string to the dateFromString
function I get nil
. I haven't read anywhere that things have changed since the iOS 7 update, but I am current on updates if that makes a difference on whether or not this still works the same way.
This is the code I'm using to create the date from string:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale systemLocale]];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
and my date string is @"2015-08-22 13:00:00"
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:@"2015-08-22 13:00:00"];
return date;
The last line returns nil.
You need to change:
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
With:
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
Since hh
refers to the 12 hour clock and HH
refers to the 24 hour clock.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments