NSDate 独立于时区?
我有一个应用程序,可以显示某些渡轮行程的时间表。
如果我前往不同的时区 - 比如说晚 4 小时,上午 10 点的渡轮行程现在会显示为上午 6 点?
我知道这与如何根据时区处理日期有关,但我不知道如何改变这种行为。
目前,这是我获取日期并将其显示在 UILabel 上的方法:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[self.departureTime setText:[dateFormatter stringFromDate:[self.route objectForKey:@"departureTime"]]];
[self.arrivalTime setText:[dateFormatter stringFromDate:[self.route objectForKey:@"arrivalTime"]]];
[dateFormatter release];
提前感谢您的帮助。
I have an app that displays a timetable of certain ferry trips.
If I travel to a different timezone - say 4 hours behind, a 10am ferry trip now shows up as 6am?
I know this has got to do with how dates are treated based on their timezones, but I can't work out how to change that behaviour.
At the moment here's how I am getting the date and displaying it on a UILabel:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[self.departureTime setText:[dateFormatter stringFromDate:[self.route objectForKey:@"departureTime"]]];
[self.arrivalTime setText:[dateFormatter stringFromDate:[self.route objectForKey:@"arrivalTime"]]];
[dateFormatter release];
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要存储渡轮乘坐所在的时区,并根据该时区对其进行格式化。
输出:
You'll need to store the timezone that the ferry ride is taking place in and format it for that timezone.
Outputs:
你见过 NSDateFormatter 的 setTimeZone 方法,是吗?
http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/setTimeZone< /a>:(
顺便说一句,如果有渡轮跨越四个时区,我会感到惊讶;听起来像是游轮行程 我)
You have seen NSDateFormatter's setTimeZone method, yes?
http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/setTimeZone:
(b.t.w., I'd be amazed if there was a ferry that involved crossing four time zones; sounds like a cruise ship itinerary to me)
您还可以使用 NSDateComponents 类,如苹果参考所述:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtTimeZones.html#//apple_ref/doc/uid/20000185-SW1
You can also use the NSDateComponents class as described by apple's reference:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtTimeZones.html#//apple_ref/doc/uid/20000185-SW1
不要将 NSDate 值与 NSLog 等格式化输出混淆。 NSDate 是 GMT,Apple 的文档:
输出:
Don't confuse an NSDate value with a formatted output like NSLog. NSDate is GMT, Apple's docs:
Output: