Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dateFromString returns nil

This is my NSString,

myDate : 2011-06-07 11:23:47  0000

And this is the code,

NSTimeZone *currentDateTimeZone = [NSTimeZone defaultTimeZone];
NSDateFormatter *currentDateFormat = [[NSDateFormatter alloc]init];
[currentDateFormat setTimeZone:currentDateTimeZone];
[currentDateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *tempDate = [currentDateFormat dateFromString:second];

Still tempDate gets the value nil.
Can anyone help?

like image 876
mayuur Avatar asked Jun 07 '11 12:06

mayuur


3 Answers

How did you get the date string? It needs to be 2011-06-07 11:23:47 +0000 for this to work.

like image 91
Deepak Danduprolu Avatar answered Sep 30 '22 13:09

Deepak Danduprolu


Try this, This is the function to get Date from String .

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:@"EEE, dd MMM yy HH:mm:ss"];
NSDate *convertedDate = [df dateFromString:stringDate];
[df release];
// [convertedDate description]
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setAMSymbol:@"AM"];
[formatter setPMSymbol:@"PM"];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm:a"];
return [formatter stringFromDate:convertedDate];
like image 29
Sujal Avatar answered Sep 30 '22 14:09

Sujal


What I think is, it not related to TimeZone but the device 24hours settings. Developer has to check if the device timings are of 24hours then they should use HH, and if the device timings are of 12hours then they should use hh.

like image 23
Zahur Avatar answered Sep 30 '22 14:09

Zahur