I have a date in NSString format with this content: 29/02/2012 12:00:00 AM
I’m trying to convert it to NSDate object in order to make comparisons using:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy hh:mm:ss"];
But when I use dateFromString: method I’m getting a null value.
What I’m missing?
Thanks.
-EDIT-
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSLog(@"INFO -> Test Date: %@",[df dateFromString:testQuestions.endDate]);
NSLog(@"INFO -> Test Date String: %@",testQuestions.endDate);
if ([[df dateFromString:testQuestions.endDate] earlierDate:[NSDate date]])
{
currentTestIsBeingSolved = NO;
}
Log:
2012-04-09 22:15:29.073 Otsuka On[27184:707] INFO -> Test Date: (null)
2012-04-09 22:15:31.816 Otsuka On[27184:707] INFO -> Test Date String: 29/02/2012 12:00:00 AM
EDIT–
As @trudyscousin suggested I’ve started a new project and added this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSLog(@"INFO -> Test Date: %@",[df dateFromString:@"29/02/2012 12:00:00 AM"]);
...
Now I’m not getting null but the date is not the same:
2012-04-09 22:26:19.189 TestDate[54705:207] INFO -> Test Date: 2012-02-28 23:00:00 +0000
What is going on with the dates?
Any config in Xcode?
Thanks.
The fact that you’ve suffixed your date and time with “AM” is what is doing it. You have to provide for that as well: