1

I have such string: "Tue, 22 Oct 2013 1:59 pm EEST" i am trying to set these parsing rules:

[formatter setDateFormat: @"EEE, dd MMM yyyy hh:mm a z"];

but this returns nil when i perform:

[formatter dateFromString: @"Tue, 22 Oct 2013 1:59 pm EEST"];

What's the right regular expression for such format?

Nikita Shytyk
  • 408
  • 7
  • 16
  • Just to tip, will not fix your issue, you should set the local of the dateformatter to english. Because if your iOS device is net set to english your dateformatting will fail. – rckoenes Oct 22 '13 at 12:14
  • 2
    possible duplicate of [Nsdateformatter + Timezone issue](http://stackoverflow.com/questions/18763420/nsdateformatter-timezone-issue) – rckoenes Oct 22 '13 at 12:16
  • 1
    Yeah, it may be a locale problem. Since you are using English terms you should use an English/US locale. – Hot Licks Oct 22 '13 at 12:27

2 Answers2

4
 NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEE, dd MMM yyyy hh:mm a zzz"];
    [formatter setLocale:locale];
    NSDate* date = [formatter dateFromString:@"Tue, 22 Oct 2013 1:59 PM EEST"];
    NSLog(@"date: %@",date);

O/P:-date: 2013-10-22 10:59:00 +0000
Balu
  • 8,470
  • 2
  • 24
  • 41
1

hh is a padded hour ("01" in your case) but your string doesn't have padded hours (just "1" ) so it should be just h instead.

You can see a practical examples of the different formatting components below (output from this GitHub gist). The date being formatted is 1987-08-27 15:24:03

format  result
--------------
    yy  87
  yyyy  1987
     M  8
    MM  08
   MMM  Aug
  MMMM  August
    dd  27
    HH  15
    hh  03    // <--.
     h  3     // <--'--- note the difference
     a  PM
    mm  24
     m  24
    ss  03
     s  3
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205