0

Possible Duplicate:
NSString to NSDate

I have a date in format Mon Jan 14 14:00:00 CET 2013 I try to convert it to NSDate:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = @"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];

but it doesn't work and my date is nil Input data is in en-GB locale, my device's locale is nb-NO

Any suggestions?

Community
  • 1
  • 1
Oleg
  • 2,984
  • 8
  • 43
  • 71
  • Looks like dateFromString is depricated. See this [SO answer](http://stackoverflow.com/questions/3402367/nsdate-datefromstring-deprecated) – Nick T Jan 15 '13 at 11:02
  • 1
    @NickThorne that's the `NSDate` class method not the `NSDateFormatter` instance method. – Carl Veazey Jan 15 '13 at 11:03

6 Answers6

2

You're missing day in your format:

[df setDateFormat:@"EEE MMM dd HH:mm:ss zzz y"];

If it was not a typo, then next thing is to set proper locale so formatter will recognise CET timezone, for example en-GB will fix that:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
// that will fix the problem with not recognized CET timezone
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-GB"]];
[df setDateFormat:@"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = @"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
lupatus
  • 4,208
  • 17
  • 19
  • it was a typo :-) doesn't work with dd as well – Oleg Jan 15 '13 at 11:06
  • Try setting NSTimeZone *cet = [NSTimeZone timeZoneWithAbbreviation:@"Europe/Paris"]; [dateFormatter setTimeZone:cet]; – HRM Jan 15 '13 at 11:11
  • still returns nil..can it be a reason that in my current locale Mon is man and Jan is jan ? – Oleg Jan 15 '13 at 11:24
  • its not case sensitive, which locale did you use? – lupatus Jan 15 '13 at 11:27
  • 1
    anyway, if you'll use en-GB then CET is recognised, for en-US it won't be recognised and will return nil, I'm not sure what else you can use than en-GB – lupatus Jan 15 '13 at 11:31
  • region settings on my iPhone are set to Norwegian format, data input is in En format – Oleg Jan 15 '13 at 11:32
  • and if you'll try to set en-GB for that formatter (like in example above) then it doesn't work? – lupatus Jan 15 '13 at 11:35
  • didn't see "locale fix", everything worked like a charm! :-) Thank you! – Oleg Jan 15 '13 at 11:41
1

I think your Time Zone is wrong. Just use this code , it will work Perfectly :

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"EEE MMM dd HH:mm:ss zzz y"];
    NSString *dateString = @"Mon Jan 14 14:00:00 EDT 2013";
    NSDate *date = [df dateFromString:dateString];
    NSLog(@"date :: %@",date);

It will log Output as :

date :: 2013-01-14 18:00:00 +0000

EDIT :

I found Something for you : NSDateFormatter doesn't parse some timezones

You can solve this by using en_GB Locale , as stated : "These abbreviations do still work with the en_GB locale" in Working with Date and Time in Cocoa .

Bhavin
  • 27,155
  • 11
  • 55
  • 94
1

Try to use this function

 - (NSDate*) dateFromString:(NSString*)aStr
{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
    //[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss a"];
    [dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSLog(@"%@", aStr);
    NSDate   *aDate = [dateFormatter dateFromString:aStr];
    [dateFormatter release];
    return aDate;
}

I hope this will helps u.

Satish Azad
  • 2,302
  • 1
  • 16
  • 35
0

Simply "CET" is not a recognized time zone by NSDateFormatter.

Also the date/tine is over specified, best to not try include the day or week (Mon).

Here is an example that demonstrates working code with a recognized timezone:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EEE MMM dd HH:mm:ss zzz yyyy"];
NSString *dateString = @"Mon Jan 14 14:00:00 EST 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(@"date: %@", date);

NSLog output

date: 2013-01-14 19:00:00 +0000

NSLog(@"abbreviationDictionary: %@", [NSTimeZone abbreviationDictionary]);

does show

CET = "Europe/Paris";

so this looks like an Apple bug in NSDateFormatter.

Report the bug at: Apple Bug Reporter

zaph
  • 111,848
  • 21
  • 189
  • 228
0

CET is not recognised

Try this :-

NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"EEE MMM dd HH:mm:ss yyyy"];
    NSString *dateString = @"Mon Jan 14 14:00:00 2013";
    NSDate *date = [df dateFromString:dateString];
    NSLog(@"%@",date);

Hope it helps you

P.J
  • 6,547
  • 9
  • 44
  • 74
-1

You can use:

[dateFormatter setDateStyle:NSDateFormatterShortStyle];

If you still want to custom your date format try this one:

yyyy-MM-dd HH:mm:ss ZZZ

Because can't invent your own formatted string syntax and expect it to work; you need to actually use a documented format as the documentation points it out : Formatters in OS X v10.8 and iOS 6.0 use version tr35-25.

-> https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html

If you are curious: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Loris1634
  • 659
  • 4
  • 5