iPhone - NSDate - NSString - NSDate 之间的转换
我使用以下代码来获取 NSDate,将其转换为 NSString,然后返回 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);
但我得到的输出是不同的:
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
为什么将 NSDate 转换为 NSString 并从 NSString 返回到 NSDate 时会有所不同?
I'm using following code to get NSDate, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是 :
使用:
Instead of :
use: