0

I always have trouble with this, and keep seem to find a good resource to learn this exhaustively. I am trying to use the date formatter in Xcode objective C, but I am not setting the date format correctly. Here is my data:

Fri, 27 Jun 2014 12:33:18 +0000

Can someone assist me or point me in the right direction? Trying the below code, but it is not working.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormatter setDateFormat:@"EEE, d LLL yyyy HH:mm:ss Z"];

Update: the format below might help, as it shows some zero padding on the day.

Wed, 02 Jul 2014 11:47:35 +0000
Noah Labhart
  • 157
  • 4
  • 18
  • Your format is wrong, but two other points: 1) the [locale "feature"](http://stackoverflow.com/q/6613110/581994) can bite you, and 2) the date formatter is a little fussy about reading dates with the day-of-week included, and you're often better off substringing the date to remove day-of-week. – Hot Licks Jul 03 '14 at 01:36
  • Thanks for the note. Is there a way to calm the fussiness? If not, i like your idea of trimming the day-of-week off. – Noah Labhart Jul 03 '14 at 16:32
  • Simply substring to the comma, to remove the day of week. But fixing the locale problem and setting some of the NSDateFormatter default settings may help -- I've never been in a situation where I needed to track down the culprit that badly. – Hot Licks Jul 03 '14 at 16:48
  • Makes sense I'll go that route for sure. Just want to understand fully, which is why I'm pushing for a solid answer. Are you suggesting that I remove the setLocale line of code as well? – Noah Labhart Jul 03 '14 at 18:54
  • It's best to use the "en_US_POSIX" locale, since otherwise you can get bit by the "locale feature". Some have mumbled that playing with, I think, the "style" settings can have some effect on the day-of-week parsing issue, but I've never seen it nailed down reliably, since the whole issue is intermittent. – Hot Licks Jul 03 '14 at 20:40

1 Answers1

2

According to the Unicode Technical Standard #35, "LLL" is the date format for a "stand-alone month" which is "a month name without an associated day number". You should use "MMM" instead:

[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];

Example:

NSString *str = @"Fri, 27 Jun 2014 12:33:18 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:str];
NSLog(@"%@", date);
// Output: 2014-06-27 12:33:18 +0000
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Unfortunately, it doesn't seem to be working. I am still getting a null date field when I create a date from the dateFormatter object. Any other thoughts? – Noah Labhart Jul 03 '14 at 00:28
  • I discovered that the format might not be slightly clear from my original post. I will update it accordingly. – Noah Labhart Jul 03 '14 at 01:31
  • Yeah, bookmark the date formatter spec link above, as you'll probably need to refer to it frequently. – Hot Licks Jul 03 '14 at 01:37
  • @nlabhart: Do you convert a NSDate to a NSString or NSString to NSDate? Is "Wed, 02 Jul 2014 11:47:35 +0000" is *input* or the *output*? – Martin R Jul 03 '14 at 17:25
  • @MartinR I am converting NSString to NSDate. The format "Wed, 02 Jul 2014 11:47:35 +0000" is the NSString. – Noah Labhart Jul 03 '14 at 18:53
  • @nlabhart: Converting the string "Wed, 02 Jul 2014 11:47:35 +0000" to NSDate worked without problems in my test program, with the date format "EEE, d MMM yyyy HH:mm:ss Z" suggested in my answer. (Actually it worked with your date format "EEE, d LLL yyyy HH:mm:ss Z" as well.) - What result do you get? – Martin R Jul 03 '14 at 18:59
  • Strange - I am still getting nil with both formats. Can you post the code you are using, if you don't mind? – Noah Labhart Jul 03 '14 at 19:02
  • @nlabhartL: Any progress? – Martin R Jul 07 '14 at 08:11