Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter stringFromDate keeps returning nil

I'm having a strange issue with a NSDateFormatter. Off course I'm doing something wrong here, but I can't figure out what it is, must be something small and stupid...

I have a date I want to format. The date is 2012-05-06 16:52:40 and is contained in an NSDate object, it is properly initialized, I checked that in the debugger.

The following code however:

NSLog(@"%@", [myDate description]);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd-MM-yyyy";
NSString *myDateAsString = [dateFormatter stringFromDate:myDate];
NSLog(@"%@", myDateAsString);

prints:

2012-05-06 16:52:40
(null)

I really don't see the error in my code. Any hints?

like image 496
Rens Verhage Avatar asked Nov 20 '25 17:11

Rens Verhage


1 Answers

myDate is actually an NSString and not an NSDate. Otherwise, the description would contain the time zone too: 2012-05-06 16:52:40 +0000.

Try the following to check that:

NSLog(@"%@", [myDate class]);
like image 188
sch Avatar answered Nov 23 '25 08:11

sch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!