سؤال

I Know it may be got answered a lot here but i think i have a different case

I'm getting the string date in the following format

2014-04-14 16:04:07 +0000

And using the following code

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *startDate = [dateFormat dateFromString:st];

But startDate always give me nil

هل كانت مفيدة؟

المحلول

You should handle zone too. Correct variant:

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

Also consider to use nice lib for date formatting ISO8601DateFormatter. It will handle your format automatically.

نصائح أخرى

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

If that format does not match the format of the date string exactly, the result of dateFromString: is nil.

It doesn't so it is.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top