My string is something like this, 2012-12-08 17:00:00.0
. Now I am trying to retrieve date from this string by using NSDate formatter. My code is
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-dd-MM hh:mm:ss"];
NSDate *date1 = [df dateFromString:@"2012-12-08 17:00:00.0"];
NSLog(@"%@ %@",date1);
[df release];
But all I am getting is null value alone..
I have tried several types to dateFormatter like, yyyy/mm/dd hh:mm:ss
, YYYY-DD-MM HH:MM:SS
.. but still no use.. Any suggestion will be appreciated.
Assuming that you have just put an empty string literal in the post by mistake and your actual dates are as described in your post, you need to use
HH
instead ofhh
to parse hours in 24-hour system. You also need to add.S
to your format string to parse the last zero of your time string:This produces the expected output (the difference in the hours is due to timezone difference):
There is a duration gap after seconds, and also you had missed strDate in dateformatter input.
NSDate *date1 = [df dateFromString:@""];
you need to pass string in the your above line
Do this: