NSFormatter dateFromString 小时问题!
当我尝试将 NSString 转换为 NSDate 时... setDateFormat 又给我多返回了 3 个小时!
示例
NSDateFormatter *inFormat2 = [[NSDateFormatter alloc] init];
[inFormat2 setDateFormat:@"MM/dd/yyyy HH:mm:ss"]; //MM/dd/yyyy/
NSDate *seekLightStart = [inFormat2 dateFromString:@"01/01/2001 06:37:35"];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
NSDate *formatterDate = [inputFormatter dateFromString:@"1999-07-11 at 10:30"];
seekLightStart --> 01/01/2001 9:37:35 格式化程序日期 ---> 1999/07/11 13:30
为什么??
When i try to convert a NSString to NSDate... setDateFormat return me 3 hours more!
Example
NSDateFormatter *inFormat2 = [[NSDateFormatter alloc] init];
[inFormat2 setDateFormat:@"MM/dd/yyyy HH:mm:ss"]; //MM/dd/yyyy/
NSDate *seekLightStart = [inFormat2 dateFromString:@"01/01/2001 06:37:35"];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
NSDate *formatterDate = [inputFormatter dateFromString:@"1999-07-11 at 10:30"];
seekLightStart --> 01/01/2001 9:37:35
formatterDate ---> 07/11/1999 13:30
why ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSDateFormatter 有一个 timeZone 属性。当您使用 -dateFromString: 时,它使用此属性和其他状态属性来生成生成的 NSDate 对象。我猜默认时区不是您的时区,而是错误的根源。
NSTimeZone: http:// developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTimeZone_Class/Reference/Reference.html
NSDateFormatter has a timeZone property. When you use -dateFromString:, it uses this and the other state properties to generate the resulting NSDate object. I would guess that the default time zone is not your time zone and is the source of your error.
NSTimeZone: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTimeZone_Class/Reference/Reference.html