NSDateFormatter dateFromString: returns nil [duplicate] - ios

This question already has answers here:
-[NSDateFormatter dateFromString:] returns nil
(3 answers)
Closed 5 years ago.
I am having problems with NSDateFormatter's dateFromString method.
I have the following date: Tue, 12 Jul 2011 20:18:26 GMT , but NSDateFormatter doesn't seem to recognize it and returns (null).
Here's what I tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:MM:SS"];
NSDate *date = [dateFormatter dateFromString:#"Tue, 12 Jul 2011 20:18:26 GMT"];
[dateFormatter release];
NSLog(#"Date: %#",date);
What am I doing wrong?

In this case you need to set dateFormat to match the format of the date in the string. Try following:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"ddd, dd MMM YYYY HH:MM:SS ZZZ"];
NSDate *date = [dateFormatter dateFromString:#"Tue, 12 Jul 2011 20:18:26 GMT"];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:MM:SS"];
NSString *newFormattedDate = [dateFormatter stringFromDate:date];
NSLog(#"Date: %# || %#",datem newFormattedDate);

Your problem is that your date format #"dd/MM/yyyy HH:MM:SS" doesn't match your date string #"Tue, 12 Jul 2011 20:18:26 GMT". You need to use a different date format.

The string you are parsing does not match the format string that you set on your NSDateFormatter. The format you entered is:
dd/MM/yyyy HH:MM:SS
...which matches strings like:
"15/07/2011 10:30:01"
If you want it to match Tue, 12 Jul 2011 20:18:26 GMT, then you need to change the format. You might have better luck with:
[dateFormatter setDateFormat:#"E, dd MMM yyyy HH:MM:SS z"];
You may find this needlessly complex documentation helpful.

Related

NSDateFormatter returns nil sometimes

I created a NSDateFormatter but sometimes it creates an NSDate out of a given string sometimes it doesn't on the same device (Simulator).
This is my code:
NSString *lastModifiedString = [metaData objectForKey:#"Last-Modified"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss Z"];
NSDate *onlineModDate = [dateFormatter dateFromString:lastModifiedString];
For Example for the string
"Fri, 09 Jan 2015 15:08:01 GMT" it returns nil but for "Fri, 12 Dec 2014 09:15:52 GMT" it returns the right NSDate.
I tried to add parenthesis to the dateformat and I tried to set locale to "en_US_POSIX" and i tried a little z instead of a big one but neither of it worked.
What am I doing wrong? Thanks for your help.
NSDateFormatter follows the Unicode standard for date and time patterns. Use 'H' for the hour on a 24-hour clock:
So in your code, the correct way should be:
NSString *lastModifiedString = [metaData objectForKey:#"Last-Modified"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *onlineModDate = [dateFormatter dateFromString:lastModifiedString];

ios NSDateFormatter date from string using literal date

I am trying to create a date out of my date string that goes:
Tue, 29 Jul 2014 17:22:30 +0200
I am using this code but it still gives me null as I try to dateFromString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd LLL YYYY hh:mm:ss +0200"];
NSLog(#"The date: %#", [dateFormatter dateFromString:bazosItem.itemDate]);
so the output is:
The date: (null)
I have been searching for the right dateFormat in the internet and have rechecked it multiple times and I have no idea what is going wrong with the date.
There are some mistakes in your format, first the YYYY should be yyyy second the hh is for AM/PM hour not 24 hours for this you should use HH.
Third and most important you should the the date format in which language the date is by adding a locale:
NSString *temp = #"Tue, 29 Jul 2014 17:22:30 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setDateFormat:#"EEE, dd LLL yyyy HH:mm:ss +0200"];
NSLog(#"The date: %#", [dateFormatter dateFromString:temp]);
Your date format seems to be incorrect. Try the following
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss +0200"];

Xcode Date Formatting - Format from RSS

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
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

How to convert mail server date string to nsdate

I am getting date string form mail server like this. Thu, 31 Dec 2009 14:32:15 +0580.
I want to convert this date string to date.
Here's my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *inputstring=#"Mon, 3 sep 2012 08:32:39 +0580";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
[dateFormatter setLenient:YES];
NSLocale *enUS = [[NSLocale alloc]initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:enUS];
NSDate *result = [dateFormatter dateFromString:inputstring];
NSLog(#"test==%#",result);
}
I am getting output null.
Excepted Output : 2012-09-03 03:02:39 +0000
First problem: You are not trying to read the "Tue" on the string. (Add "EEE, " to the front of your format)
Second and bigger problem: +0580 is not a valid time zone. There was a PHP bug a few years ago that mistakenly returned IST (+0530) as +0580. +0580 makes no sense. It means 5 hours and 80 minutes. So you can do one of two things: Either replace +0580 with +0530 before you process it or set the date formatter time zone to be IST and remove the +0580 from the string.
I see you accepted another answer, but that answer "works" because it fails to parse the final part and ignores the time zone. I ran it and got 2013-07-09 08:32:38 +0000 (Which is not the same as 2013-07-09 08:32:39 +0580)
Removing the + in the accepted answer's format causes the formatter to parse correctly, but you will get null because the timezone is invalid. Changing the time zone to +0530 gives the expected result of 2013-07-09 03:02:39 +0000
Replace
[dateFormatter setDateFormat:#"d MMM yyyy HH:mm:ss ZZZ"];
with
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss zzzz"];
Just copy and paste the below code in your project and run, see the result
NSString *dateString = #"Tue, 9 Jul 2013 08:32:39 +0580";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MM yyyy hh:mm:ss +zzzz"];
NSDate *dateFromString;
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"test==%#",dateFromString);
#Raviteja Kammila use following code for date format like 2013-07-09 03:02:39 +0000
NSString *dateString = #"Tue, 9 Jul 2013 08:32:39 +0580";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MM yyyy HH:mm:ss +zzzz"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"final date : %#",date);

Date Formatting gives null

I have string "24 Jan 2012 09:21:21 +0000" and I want to make it a NSDate object as
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate* date = [dateFormatter dateFromString:dateStr];
NSLog(#"%#",date);
gives me (null)
What i should do to get date in the format I want.
Since you have a specific date format, you can set your formatter up to match it.
dateString = #"24 Jan 2012 09:21:21 +0000";
[formatter setDateFormat:#"d MMM yyyy HH:mm:ss Z"];
date = [formatter dateFromString:dateString];
NSLog(#"Date: %#", date);
You can see the list of date specifiers here.
24 Jan 2012 09:21:21 +0000
[dateFormatter setDateFormat:#"dd MMM yyyy HH:mm:ss ZZZ"];
I don't have an obj C environment to verify, but your date string is "year month day" and the input you want to handle is "day (short)month year"

Resources