I want to set date in this format MMM dd, yyyy HH:mm, I hede done code for this but there is some thing wrong which I don’t know.
NSDate *date = [NSDate date];
[format setDateFormat:@"MMM dd, yyyy HH:mm"];
NSLog(@"My date with out format = %@",date);
date = [format dateFromString:[format stringFromDate:date]];
NSLog(@"My date is = %@",date);
OUTPUT
My date with out format = 2012-06-22 09:53:46 +0000
My date is = 2012-06-22 09:53:00 +0000
Please help me where I am doing wrong.
Thanks in advance.
Firstoff,
NSDatedoes not know any thing about the formatting of the date. Thats whereNSDateFormattercomes in.What you are doing here is create a string for the given format, which will also result in removing the timezone. and than create a new date from that string. Since it no longer has the timezone it will result in a date with
GMTas time zone.But this will still result in a
NSDatewhich holds, seconds and has a default time zone set toGMT.There is no need to make a new date object, if you want to display a date just take the string which the
stringFromDate:method returns. If you do it like this it will work: