0

I am having problems with conversion of NSString object to NSDate. Here is what I want to do: I am downloading an RSS Message from the internet. I have the whole message parsed by NSXMLParser. After that, I have parts like , or saved to particular NSStrings. I want to convert element (that includes publication date of RSS Message) to NSDate so that I could perform some operations on it like on a date object (e.g. sorting, showing on a clock etc.). Here is the way my looks like:

"Wed, 25 Sep 2013 12:56:57 GMT"

I tried to convert it to NSDate in this way:

*//theString is NSString containing my date as a text
NSDate *dateNS = [[NSDate alloc] init];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss ZZZ"];
dateNS = [dateFormatter dateFromString:theString];*

However, after doing above code, dateNS always appear to be (null).

My question is simple: what is the right way to convert NSString with date formatted like this to NSDate object?

By the way, I have seen the website http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns It seems that there are many ways to format particular date, but I could not find what I am doing wrong.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
tom55
  • 13
  • 3
  • You need `HH` for the hour in 24-hour format. – rmaddy Sep 28 '13 at 16:56
  • OK, it works right now. I did not set the Time Zone and Locale (by the way, I still don't know why do I have to do this). – tom55 Sep 28 '13 at 18:57
  • But I got another problem: After converting my date from string to NSDate, it is moved in time by 3 hours, e.g. 01.01.2013 11:30 -> 01.01.2013 14:30. Why? Is it because of wrong LocaleIdentifier (I am in Poland and i set LocaleIdentifier for en_GB - I have some problem with changing it to pl_PL. TimeZone is changed to Europe/Warsaw). – tom55 Oct 05 '13 at 19:23

2 Answers2

0

NSDate * dateNS = [[NSDate alloc] init]; is useless, you don't need to allocate any date object.

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSLog(@"%@", [dateFormatter dateFromString:@"Wed, 25 Sep 2013 12:56:57 GMT"]);

Outputs the date correctly, are you sure theString isn't nil?

pNre
  • 5,376
  • 2
  • 22
  • 27
0

Your problem is the your date formatter has not identical fort as your date string: You should set date formatter the same format like your date string

My Example:

// Convert string to date

    NSString *beginString = @"Sat, 30 Dec 2013 14:45:00 EEST";
    //beginString = [beginString stringByReplacingOccurrencesOfString:@"EEST" withString:@""];
    //beginString = [beginString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Helsinki"]];
    [dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
    dateFromString = [dateFormat dateFromString:beginString];

    //NSLog(@"Begin string: %@", beginString);

    //NSLog(@"not formated: %@", dateFromString);

    // Convert Date to string

    [dateFormat setTimeZone:[NSTimeZone localTimeZone]];
    [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ru_RU"]];
    [dateFormat setDateFormat:@"dd MMMM yyyy"];
    myStrDate = [dateFormat stringFromDate:dateFromString];
    [currentTitle setPubDate:myStrDate];
Anton
  • 3,102
  • 2
  • 28
  • 47