Question

I have an array that contains some date strings. I'm using NSDateFormatter but the problem is that it recognizes some of the dates and do not recognize others while all of them have the same format!

for example it formats: Sat, 01 Feb 2014 08:44:00 +0430

But returns null for: Wed, 29 Jan 2014 17:40:00 +0430

this is the code i used:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss xx"];
NSDate *date = [dateFormat dateFromString:dateInput];
Était-ce utile?

La solution

Use this date format [dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss xx"];

Autres conseils

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss xx"];
NSDate *date = [dateFormat dateFromString:dateInput];

Note:

HH is denoted by 24 hour format

hh is is denoted by 12 hour format

Try this

[dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss xx"];

Because, HH is syntax for 24 hour format which is suitable for your criteria.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top