NSString to NSDate with setDateFormat - parsing

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.

Related

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

NSDateFormatter not giving expected results

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"]);

Convert NSString to NSDate, adding time

I have an NSString that is 05/08/2014. I want to convert that to an NSDate. However, I also need to add in time, so that the resulting NSDate looks like this:
Thu, 8 May 2014 00:00:00 -0500
The time is not important, I just need it to show midnight at the designated timezone.
I have tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:textdate.text];
[dateFormatter release];
NSLog(#"%#", dateFromString);
But the date comes back as (null).
Your date format is wrong for the first conversion. What you need is first to convert from string to date from one format and form the new date into the new string format. Something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"]; //convert the string into date (american time zone)
NSDate *theDate = [formatter dateFromString:textdate.text];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];// as #Logan suggested
NSString *newDate = [formatter stringFromDate:theDate];
EEE, d MMM yyyy HH:mm:ss ZZZ
Your time zone code was lowercase instead of uppercase.
zzz corresponds to PDT
ZZZ corresponds to -0500
UTS 35

Converting NSString to NSDate

I have to convert Jan 24, 2014, 7:27:56 PM to NSDate
but facing problem to convert it due to Upper case PM/AM , i am using this code , but it giving me nil.
NSString *stringDate = #"Jan 24, 2014, 7:27:56 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSDate *date=[dateFormatter dateFromString:stringDate];
NSLog(#"Date 1 : %#",date); //2013-02-28 12:00:00 +0000
Your formatter string contains sequence "yyyy hh", however the stringDate has "yyyy, hh". Try this:
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm:ss a"];

Strange date format behaviour , iOS

I am trying to convert a date from this :
2012-06-26 12:00:00 +0000
to this :
Jun 26, 2012 12:00:00 AM
If i use this code , it works :
NSString *inputString = #"2012-06-25 12:00:00 +0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];//input
NSDate *date = [formatter dateFromString:inputString];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:date];
NSLog(#"Output Date is: %#" , outputString);
and the date printed is correctly this : Jun 26, 2012 12:00:00 AM
However the thing gets really strange when i dont hardcode the date , but use a function to get it. The code :
NSString *inputString2 = [self.selectedDate description];
NSLog(#"%#",inputString2);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];//input
NSDate *date = [formatter dateFromString:inputString2];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:date];
NSLog(#"Output Date is: %#" , outputString);
When i print in the beginning of the code the inpuString2 variable just to make sure that the date is correctly delivered by the function there i have this: 2012-06-25 12:00:00 +0000 which is the correct date. But now the output date is nil when printed!!
Any ideas?? how is this possible??
Its the date format string for formatter
You have
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];
Try instead
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
Note: HH not hh and Z not z.
RE: the second formatter string - you could change that to
[formatter setDateFormat:#"MMM dd, yyyy HH:mm:ss a"];
(but I thought you were just asking about the nil)
Am assuming selectedDate date is NSDate, instead of converting use the following
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:self.selectedDate];
NSLog(#"Output Date is: %#" , outputString);
This may not answer your question, but it should give you correct date

Resources