Strange date format behaviour , iOS - 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

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

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

how to convert string into date format in iOS?

I have string from web service like 12/31/2013 09:12:15 A.M.
Now I Want convert it like 12 Dec 2013 09:12:15 A.M.
with the use of NSDAteFormatter in iOS
I'm giving you the answer, but downvoting your question for being too elementary and whose solution could easily found with a simple search.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [formatter dateFromString:myDateString];
[formatter setDateFormat:#"dd MMMM yyyy aa:mm:ss a"];
NSString *newDate = [formatter stringFromDate:date];
NSString *dateStr = #"12/31/2013 09:12:15 AM";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];

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

Changing date format on iOS

I have some dates in this format :
2012-06-26 12:00:00 +0000
and what i want to do is convert them in this format :
Jun 26, 2012 12:00:00 AM
Is there an easy way to do this on obj-c , or i must parse everything hardcore , and make my own function for this? If yes , any ideas how this would look like , cause i am new on iOS.
Thank you very much for reading my post :D
To convert from 1 string to the other use the following
NSString *inputString = #"2012-06-26 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 : %#",outputString);
First, you should convert your string (with a NSDateFormatter ) in a NSDate and than convert it back to a NSString (still with the same NSDateFormatter) with the target format.
Here is an example of a NSDateFormatter :
NSString *dateString = #"2013-04-01T22:00:00Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone];
NSDate *theDate = [dateFormatter dateFromString:dateString];

Resources