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];

However, tempDate gets the value nil.
Can anyone help?

+5
source share
4 answers

How did you get the date string? For this you need 2011-06-07 11:23:47 +0000.

+3
source

Try this. This is a function to get the date from a 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];
+2
source

, TimeZone, 24 . , 24 , HH, 12 , hh.

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

autorelease ZZZZ Z.

0

All Articles