NSDate 独立于时区?

发布于 2024-12-09 03:57:11 字数 546 浏览 1 评论 0原文

我有一个应用程序,可以显示某些渡轮行程的时间表。

如果我前往不同的时区 - 比如说晚 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三生路 2024-12-16 03:57:11

您需要存储渡轮乘坐所在的时区,并根据该时区对其进行格式化。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"]; 

NSDate *now = [NSDate date];   
NSLog(@"now:%@", [dateFormatter stringFromDate:now]);

NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:(-8 * 3600)];     
[dateFormatter setTimeZone:timeZone];
NSLog(@"adjusted for timezone: %@", [dateFormatter stringFromDate:now]);

输出:

2011-10-10 20:42:23.781 Craplet[2926:707] now:20:42
2011-10-10 20:42:23.782 Craplet[2926:707] adjusted for timezone: 16:42

You'll need to store the timezone that the ferry ride is taking place in and format it for that timezone.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"]; 

NSDate *now = [NSDate date];   
NSLog(@"now:%@", [dateFormatter stringFromDate:now]);

NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:(-8 * 3600)];     
[dateFormatter setTimeZone:timeZone];
NSLog(@"adjusted for timezone: %@", [dateFormatter stringFromDate:now]);

Outputs:

2011-10-10 20:42:23.781 Craplet[2926:707] now:20:42
2011-10-10 20:42:23.782 Craplet[2926:707] adjusted for timezone: 16:42
相思故 2024-12-16 03:57:11

你见过 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)

怪异←思 2024-12-16 03:57:11

您还可以使用 NSDateComponents 类,如苹果参考所述:

如果您需要创建独立于时区的日期,则可以将日期存储为 NSDateComponents 对象 - 只要存储对相应日历的一些引用即可。

在 iOS 中,NSDateComponents 对象可以包含日历、时区和日期对象。因此,您可以将日历与组件一起存储。如果您使用 NSDateComponents 类的 date 方法来访问日期,请确保关联的时区是最新的。

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:

If you need to create a date that is independent of timezone, you can store the date as an NSDateComponents object—as long as you store some reference to the corresponding calendar.

In iOS, NSDateComponents objects can contain a calendar, a timezone, and a date object. You can therefore store the calendar along with the components. If you use the date method of the NSDateComponents class to access the date, make sure that the associated timezone is up-to-date.

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtTimeZones.html#//apple_ref/doc/uid/20000185-SW1

内心激荡 2024-12-16 03:57:11

不要将 NSDate 值与 NSLog 等格式化输出混淆。 NSDate 是 GMT,Apple 的文档:

NSDate唯一的原始方法,timeIntervalSinceReferenceDate,
为 NSDate 接口中的所有其他方法提供基础。
此方法返回相对于绝对参考的时间值
日期——格林尼治标准时间 2001 年 1 月 1 日的第一时刻。

NSTimeInterval referenceInterval = [[dateFormatter dateFromString:@"1 January 2001 GMT"] timeIntervalSinceReferenceDate];
NSLog(@"referenceInterval: %f", referenceInterval);

NSTimeInterval estInterval = [[dateFormatter dateFromString:@"1 January 2001 EST"] timeIntervalSinceReferenceDate];
NSLog(@"estInterval:       %f", estInterval);

输出:

referenceInterval: 0.000000  
estInterval:   18000.000000

Don't confuse an NSDate value with a formatted output like NSLog. NSDate is GMT, Apple's docs:

The sole primitive method of NSDate, timeIntervalSinceReferenceDate,
provides the basis for all the other methods in the NSDate interface.
This method returns a time value relative to an absolute reference
date—the first instant of 1 January 2001, GMT.

NSTimeInterval referenceInterval = [[dateFormatter dateFromString:@"1 January 2001 GMT"] timeIntervalSinceReferenceDate];
NSLog(@"referenceInterval: %f", referenceInterval);

NSTimeInterval estInterval = [[dateFormatter dateFromString:@"1 January 2001 EST"] timeIntervalSinceReferenceDate];
NSLog(@"estInterval:       %f", estInterval);

Output:

referenceInterval: 0.000000  
estInterval:   18000.000000
纵山崖 2024-12-16 03:57:11
        NSDate *currentDateTime =  datePicker.date;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"EEE,MM-dd-yyyy HH:mm:ss"];
        NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];
        NSLog(@"%@", dateInStringFormated);
        NSDate *currentDateTime =  datePicker.date;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"EEE,MM-dd-yyyy HH:mm:ss"];
        NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];
        NSLog(@"%@", dateInStringFormated);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文