NSDateFormatter doesn't parse my string - nsdateformatter

I've got this kind of date: Sun, 30 Jun 2013 16:00:00
I've created this NSDateFormatter:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss"];
but when I use it with dateFromString it returns nil.
What's wrong?
Thank you!

Does this fix it? (Maybe your device is in a locale that doesn't understand Sun or Jun)
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];

Related

converting date to correct format

I've a webservice which gives back my date in the following way.
Wed Oct 31 11:59:44 +0000 2012
But I want it to give it back in this way
31-10-2012 11:59
I know that it should be done with a NSDateFormatter. But I don't now how to implement it in the correct way.
I've something like this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
NSDate *date = [dateFormatter dateFromString:[genkInfo objectForKey:DATE]];
Can anybody help me?
Kind regards.
Code at the moment
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"E MMM d hh:mm:ss Z y"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"dd-MM-y hh:mm"];
[f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *date2 = [f2 stringFromDate:date];
Webservice layout
"text": "KRC Genk | Zaterdag is er opnieuw een open stadiontour http://t.co/tSbZ2fYG",
"created_at": "Fri Nov 02 12:49:34 +0000 2012"
Step 1: create an NSDateFormatter to convert your string from server to an NSDate object by setting the format to the format of the "server string"
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"E MMM d hh:mm:ss Z y"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
Step 2: create another NSDateFormatter with the desired output string and convert your new NSDate object to a string object using the new NSDateFormatter
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"dd-MM-y hh:mm"];
[f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *s = [f2 stringFromDate:date];
desiredformat = s;
P.S. I'm not sure of f format, check this link
http://www.developers-life.com/nsdateformatter-and-uifont.html
There are a few issues with your format string for parsing the original date. And the locale isn't set properly. There is no need to set the timezone. That will be processed from the supplied date/time string.
NSDateFormatter *f = [[NSDateFormatter alloc] init];
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[f setLocale:posix];
[f setDateFormat:#"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
You want to use the en_US_POSIX locale whenever you are parsing (or formatting) a fixed format date that is not from or for a user. Do not use the en_US_POSIX locale to display dates or times to a user.

creating NSDate from format

What is the NSFormat required for a string like this?
Fri, 15 Jun 2012 15:37:38 GMT
I'm trying to create a NSDate object from it. thanks
Use the following
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
NSDate *date = [formatter dateFromString:#"Fri, 15 Jun 2012 15:37:38 GMT"];
NSLog([date description]); //2012-06-15 15:37:38 +0000

NSDate and NSDateFormatter return invalid CFStrinfRef

I've one problem with NSDate and NSDateFormatter.
From this NSString
NSString *startDate = #"Thu, 19 Apr 2012 13:09:56 +0000";
I would obtain a date localized with current locale like this "Thu, 19 Apr 2012 13:09".
I've elaborate this code but the 'endDate' string return nil (within the debugger obtain 'invalid CFStringRef').
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *dateFromString = [dateFormatter dateFromString:startDate];
[dateFormatter setDateFormat:#"EEE dd MMM yyyy HH:mm"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *endDate = [dateFormatter stringFromDate:dateFromString];
Where is the bug?
Alex.
It seems that the code executed in a non-US locale. I use non-US locales. I have the same nil output also.
It seems like we need to setLocale: before setDateFormat:.
Below code works ok:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
Ya its working correctly..Try removing autorelease from your dateFormatter and then try it again.
And i hope this link is same as yours
Invalid CFStringRef issue
Try this answer and change your format option with "ZZ" and not "ZZZ".

Help with date formats

What's the correct format to parse this string:
"Mon, 14 Mar 2011 15:36:00 GMT"
into a NSDate using NSDateFormatter?
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSDate *myDate = [dateFormatter dateFromString:strPubDate];
NSString *dateStr = [NSString stringWithString:#"Mon, 14 Mar 2011 15:36:00 GMT"];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSDate *date = [dateFormat dateFromString:dateStr];

return nil for dateFromString call of NSDateFormatter

I am getting nil returned for the date variable in the below code. I can't find any problem with the date format, can anyone help?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSString *dateString = [[NSString alloc] initWithFormat:#"%#", #"Mon Apr 05 04:37:28 UTC 2010"];
NSDate *date = [formatter dateFromString:dateString];
Try
NSDate *date = [formatter dateFromString:#"Mon Apr 05 04:37:28 GMT 2010"];
or
NSDate *date = [formatter dateFromString:#"Mon Apr 05 04:37:28 +0000 2010"];