Converting NSString to NSDate - ios

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

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

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.

nslocale issue in convert string to date

I'm new to xcode, pls see below code and link
enter link description here
{
NSString * TempDate = [NSString stringWithFormat:#"%# %# %#",Datelbl.text,yearString,TimeLbl.text]; //Thursday, November 21 2013 5:35 PM
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat :#"EEEE',' MMMM d yyyy hh:mm a"];
AppointmentDate = [dateFormatter dateFromString:TempDate]; //NSDate
Appointment = [dateFormatter stringFromDate:AppointmentDate]; //NSString
}
Here , appointmentDate returns this value -> 2013-11-21 12:05:00 +0000,
Appointment returns this value-> Thursday, November 21 2013 05:35 PM.
I need to get 2013-11-21 05.35 PM. So,how to manage this GMT issue.
Try this one:
NSString *date = #"Thursday, November 21 2013 3:05 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat : #"EEEE, MMMM d yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *AppointmentDate = [dateFormatter dateFromString:date];
NSLog(#"DATE--> %#",AppointmentDate);
Output:
DAT--> 2013-11-21 15:05:00 +0000
Note: NSDate will be in 24 hour format. You need to convert it into string is you want as 03:05...
Edit :
[dateFormatter setDateFormat : #"EEEE, MMMM d yyyy hh:mm a"];
String value not converting to date format

NSDate in ios conversion

i wanto convert the string 2013-03-19 13:41:57 +0000 to this format Wed Feb 27 2013 00:00:00 GMT+0530 (India Standard Time) in iOS .please help me any help will be highly appreciated.
i tried some thing like this:
NSString * clippedString = #"2013-03-19 13:41:57 +0000 ";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE MMM d yyyy HH:mm:ss Z"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:clippedString];
I accepted the challenge :)
NSString *string=#"2013-03-19 13:41:57 +0000";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
NSDate *date=[dateFormatter dateFromString:string];
[dateFormatter setDateFormat:#"EEE MMM dd yyyy HH:mm:ss zzz zzzz"];
NSLog(#"%#",[dateFormatter stringFromDate:date]);

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