开发者

iPhone - Conversion between NSDate - NSString - NSDate

开发者 https://www.devze.com 2023-02-10 14:59 出处:网络
I\'m using following code to get NS开发者_C百科Date, convert it to NSString and then back t NSDate.

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

精彩评论

暂无评论...
验证码 换一张
取 消