iPhone 中的日期格式化程序问题
我得到的 NSDate *newDate
值为 2011-08-12 13:00:00 +0000
。 现在,当我尝试通过 dateformatter 将其转换为字符串时,如下所示:
NSDateFormatter *dateFormatter= [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd h:mm:ss Z"];
NSString *datestr = [[dateFormatter stringFromDate:newDate] lowercaseString];
但是我得到的 datestr 值比 newDate 值多 5:30 小时。我不知道我错在哪里。我得到的 datestr
值为 2011-08-12 18:30:00 + 5:30
(这是不正确的)。
I am getting a NSDate *newDate
value as 2011-08-12 13:00:00 +0000
.
Now when I try to convert it to a string by dateformatter as follows:
NSDateFormatter *dateFormatter= [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd h:mm:ss Z"];
NSString *datestr = [[dateFormatter stringFromDate:newDate] lowercaseString];
But the value which I get for datestr is 5:30 hours more than the newDate
value. I don't know where I am wrong. I get value for datestr
as 2011-08-12 18:30:00 + 5:30
(which is incorrect).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它基本上是将时间转换为您当地的时区。如果您不想这样做,只需为日期格式化程序设置时间:
文档链接:
http://developer.apple.com/library/ mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimeZone_Class/
It is basically converting the time to your local time zone. if you dont want that just set the time aone for date formatter:
Documentation link:
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimeZone_Class/
看起来它一定是一个时区/UTC 的东西。日期可能以全球/通用时间返回,而您想要的是当地时间。
根据IOS文档: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html - 似乎您可以尝试包含区域设置以指定您想要的时区。
Looks like it must be a timezone / UTC thing. The date is probably being returned in global / universal time and what you want is the local time.
According to the IOS documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html - seems you could try including a locale in order to specify which time zone you want.