I'm using following code to get NS开发者_C百科Date, convert it to NSString and then back t NSDate.
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:@"MM/dd/YYYY"];
NSDate* dt = [NSDate date];
NSString* strDate = [dtFormatter stringFromDate:dt];
NSLog(@"First = %@",strDate);
NSDate* dt2 = [dtFormatter dateFromString:strDate];
NSString* strDate2 = [dtFormatter stringFromDate:dt2];
NSLog(@"Second = %@",strDate2);
But the output I'm getting is different:
2011-02-12 08:17:21.851 SF Calculation[16297:207] First = 02/12/2011
2011-02-12 08:17:21.852 SF Calculation[16297:207] Second = 12/26/2010
Why is it different when converting NSDate to NSString and back from NSString to NSDate?
Instead of :
[dtFormatter setDateFormat:@"MM/dd/YYYY"];
use:
[dtFormatter setDateFormat:@"MM/dd/yyyy"];
精彩评论