0

I have one date on NSString with format: "Jun 25, 2014 10:10:15 AM"

I need to put this date from one NSDate. But I tried some formats on date format and always value of NSDate is nil on device. If I run by Xcode on device or on simulator its work but i generate a .ipa and send to iPhone not work.

code:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM dd, yyyy HH:mm:ss a"];
NSDate * dataStringFormatted = [dateFormat dateFromString: @"Jun 25, 2014 10:10:15 AM"];

I tried formats:

@"MMM dd, yyyy HH:mm:ss a"
@"MMM dd, yyyy H:mm:ss a"
@"MMM dd, yyyy HH:mm:ss aa"
@"MMM dd, yyyy H:mm:ss aa"
@"MMM dd, yyyy HH:mm:ss aaa"
@"MMM dd, yyyy H:mm:ss aaa"
@"MMMM dd, yyyy H:mm:ss aa"
@"MMMM dd, yyyy H:mm:ss aaa"
@"MMMM dd, yyyy H:mm:ss a"
@"MMM dd, yyyy HH:mm:ss A"

With all formats always the Object NSDate is nil.

Thank.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Gian
  • 23
  • 1
  • It should be noted that NSDateFormatter is a little fussy about reading date strings containing the day of the week. Sometimes it's best to simply strip off day-of-week before parsing. – Hot Licks Jun 25 '14 at 14:36
  • probably you are not using english localisation on your device. – holex Jun 25 '14 at 15:59
  • Yes, I not used the localization and the "hh" to hours PM/AM – Gian Jun 26 '14 at 23:59

1 Answers1

6

There a two issues with your code, HH is for 24 hour dates and you should tell the formatter which language the date is going to be in:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormat setDateFormat:@"MMM dd, yyyy hh:mm:ss a"];
NSDate * dataStringFormatted = [dateFormat dateFromString: @"Jun 25, 2014 10:10:15 AM"];
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Also, one needs to be aware of the date formatter locale "feature": http://stackoverflow.com/q/6613110/581994 – Hot Licks Jun 25 '14 at 14:35
  • @HotLicks That is in the answer, as long as the date he want to parse are in english setting the local `en_US_POSIX` should do the trick. – rckoenes Jun 25 '14 at 14:45
  • Really its works. Laked attetion by the HH and i didn't know locale are needed Thank you – Gian Jun 25 '14 at 14:49