NSDateFormatter not giving expected results - ios

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMM YYYY"];
NSLog(#"formatted date %#",[formatter dateFromString:#"14 Oct 2013"]);
I am trying to format the date 14 Oct 2013, to get an NSDate object out of it. But for some reason, it is not formatted as expected.
Here's the date object that I get after logging it on console,
formatted date 2012-12-22 18:30:00 +0000
Anything wrong I am doing here?

You should use lowercase yyyy because uppercase one represent a week based calendar year and you should specify timezone:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMM yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter setTimeZone:gmt];
NSLog(#"formatted date %#",[formatter dateFromString:#"14 Oct 2013"]);

Related

NSDateFormatter returns nil while calling datefromstring method?

I'm trying to create a date object with the specified formatter but date formatter_datefromstring method returns nil. Please let me know with the clear documentation samples. The String am trying to parse is "2:00 AM PDT on September 24, 2017". Thanks in advance
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"h:mm a Z 'on' MMMM d, yyyy"];
NSLog(#"dateStr:==============> %#", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"Date:------------->%#", date);
return date;
You are using the wrong timezone specifier. Z is for timezones such as -0800. You need z for short timezone abbreviations like PDT.
Also, there is no reason to set the formatter's local to currentLocale and the timezone to systemTimeZone since those are the defaults. And the timezone of the formatter is irrelevant when the string you are parsing contains timezone information.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"h:mm a z 'on' MMMM d, yyyy"];
NSLog(#"dateStr:==============> %#", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"Date:------------->%#", date);
return date;
However, since you are parsing a fixed format date string that is in English, you really should set the formatter's locale to the special locale of en_US_POSIX. This will ensure it handles the English month name no matter the user's locale.
[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
Can you check your dateStr date format and your given format same or not. If both are not same format you will get nil object. Try dateStr format in given below example.
NSString *dateStr = #"10:23 am Z on September 30, 2017";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"h:mm a Z 'on' MMMM d, yyyy"];
NSLog(#"dateStr:==============> %#", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"Date:------------->%#", date);
In console you will get
2017-09-23 09:54:04.654597+0530 Date[4068:79926] dateStr:==============> 10:23 am Z on September 30, 2017
2017-09-23 09:54:04.657359+0530 Date[4068:79926] Date:------------->Sat Sep 30 15:53:00 2017
Use the below locale the avoid returning nil value.
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
Vote my code if it is usefull

NSDateFormatter returns nil in a simply date format change

I'm trying to convert this date:
Jun 23, 2015 7:53:04 PM
coming from a server response, to a different format using NSDateFormatter but it is constantly returning nil and (null) values in the console if I try to NSLog it. The code I'm using to do so is the following:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSDate *date = [dateFormatter dateFromString:[contentData valueForKey:#"startDate"]];
Isn't MMM dd, yyyy hh:mm:ss a the correct format to parse the date?
UPDATE
I changed the code in the following way:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSDate *date = [dateFormatter dateFromString:#"Jun 23, 2015 7:53:04 PM"];
But it still doesn't work
This is working for me.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSDate *date = [dateFormatter dateFromString:#"Jun 23, 2015 7:53:04 PM"];
NSLog(#"date: %#",date);//date: 2015-06-23 13:53:04 +0000
NSString* formattedDate = [dateFormatter stringFromDate:date];
NSLog(#"Formatted Date: %#",formattedDate);//Formatted Date: Jun 23, 2015 07:53:04 PM

iOS create date from string

How can I make this format in iOS: "Mon Jan 26 07:57:33 +0000 2015"?
I have tried this
NSString *createdAt=#"Mon Jan 26 07:57:33 +0000 2015";
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"ddd MMM dd HH:mm:ss zzzz yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter setTimeZone:gmt];
NSDate *date=[formatter dateFromString:createdAt];
but this returns me nil always
// Convert string to date
//Fri, 23 Jan 2015 14:24:24 IST
NSString *beginString = [currentFields objectForKey:#"pubDate"];
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"]];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Kiev"]];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
dateFromString = [dateFormat dateFromString:beginString];
This is my example. Your date formatter should be the same as your original string date.

Covert string with timezone to Date

Here is the date string
NSString * dateString = #"Mon Sep 29 14:40:00 2014 PET";
How to convert it into NSDate?
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setCalendar:[NSCalendar currentCalendar]];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zzz"];
NSDate* date = [dateFormatter dateFromString:dateString];
Above code doesn't work.
what is the correct date format for above case?
the problem is your timezone definition (PET), because that is not on list of the recognisable timezones (please don't ask why not); this timezone is mostly known as UTC-5 in Apple's system, therefore:
NSString *_dateString = #"Mon Sep 29 14:40:00 2014 PET";
_dateString = [_dateString stringByReplacingOccurrencesOfString:#"PET" withString:#"UTC-5"];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[_dateFormatter setCalendar:[NSCalendar currentCalendar]];
[_dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zzz"];
NSDate *_date = [_dateFormatter dateFromString:_dateString];
NSLog(#"date : %#", _date);
and the console says:
date : 2014-09-29 19:40:00 +0000
which is the correct representation of the original date in GMT+0 timezone.

NSString to NSDate with setDateFormat

I have a problem with NSDateFormatter parsing a string coming from the web.
I parse dates as string and transform them to NSDate. The problem is in choosing the correct format string.
Parsed dates have following "format":
Feb 04, 2014 8:00 AM ET
but I haven't find the correct format to transform them into an NSDate
I've tried with: EEE dd, yyyy hh:mm a but it is not working.
Code is simple:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd, yyyy hh:mm a"];
NSDate *articleDate = [formatter dateFromString:articleDateString];
Any idea?
Try below code to convert your NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm a 'ET'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
NSDate *articleDate = [dateFormatter dateFromString:#"Feb 04, 2014 8:00 AM ET"];
NSLog(#"-->%#",articleDate);
Using this page: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Change your "EEE" to "MMM" and add a "z" at the end to catch your timezone.
"EEE" is day of week, for example, "Tue", not month.

Resources