Reputation: 9830
I have an NSString
that has a date. I'm trying to convert that date to an NSDate
. When I do that I get nil
fro the NSDate
. Here is my code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"dateString = %@ date = %@", dateString, date);
Here is the output of the NSLog
:
dateString = 2015-06-16 date = (null)
What am I doing wrong, and how can I fix it?
Upvotes: 0
Views: 52
Reputation: 112873
The date format does not match the date string. It needs to be: :@"yyyy-MM-dd"
The order and other characters need to match.
yyyy for a four digit year
MM for a two digit month
dd for a two digit day
See: ICU Formatting Dates and Times
Note: NSLog()
uses the NSDate
description
method which presents date/time referenced to GMT (UTC) and NSDateFormatter
defaults to your timezone so the date displayed may be different.
Upvotes: 3