使用 initWithTimeInterval:sinceDate: 得到错误的日期:

发布于 2024-09-15 19:37:51 字数 281 浏览 2 评论 0原文

我有一个日期,2010-08-23 13:30:00 -0400,我试图在 UTC +2 时区获取它。以秒为单位的差异是 21600,但是当我调用 [[NSDate alloc] initWithTimeInterval:intervalsinceDate:sourceDate]; 时,我得到 2010-08-24 01:30:00 +0200 code> 应为 2010-08-23 19:30:00 +0200。知道我做错了什么吗?

I have a date, 2010-08-23 13:30:00 -0400 and I'm trying to get it in the UTC +2 time zone. The difference in seconds is 21600 but when I call [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];, I get 2010-08-24 01:30:00 +0200 which should be 2010-08-23 19:30:00 +0200. Any idea what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一向肩并 2024-09-22 19:37:51

日期对象代表一个特定的时刻,无论不同人的时钟当时可能表示什么。

当您想要显示日期时,时区就会涉及到,因为您通常会显示用户时钟当时会说(或正在说,或将要说)的内容。这正是您 NSLog 一个 NSDate 时发生的情况:日期的描述是以您当地时区表示的那一刻。

在 NSDate 中添加或减去不是时区转换。加法和减法产生代表时间上的其他时刻的NSDate;就您而言,您正在为从您开始的那一刻起 6 小时后的某个时刻创建一个 NSDate,而不是在地球另一端的同一时刻。

为了简单起见,时区转换是在空间中的不同点(地球表面的不同部分)查看同一时间,而日期加法和减法是计算不同的时间。

NSDate 没有时区转换,因为它一开始就不在时区中。仅当您设置时区的日期格式时,才会涉及时区。因此,要在其他时区显示该日期,只需这样做即可。通常,这意味着向您的 提供所需的时区日期格式化程序

A date object represents a specific moment in time, irrespective of what different people's clocks might have said at that time.

Time zones become involved when you want to display the date, since you'll typically display what the user's clock would have said (or is saying, or will say) at that time. This is exactly what happens when you NSLog an NSDate: The date's description is that moment of time represented in your local time zone.

Adding to or subtracting from an NSDate is not time zone conversion. Addition and subtraction produce NSDates representing other moments in time; in your case, you're creating an NSDate for a moment 6 hours later from the moment you started with, not the same moment on the other side of the planet.

To simplify, time zone conversion is looking at the same time in different points in space (different sections of the planet's surface), whereas date addition and subtraction are computing different times.

There is no time zone conversion for an NSDate, because it isn't in a time zone to begin with. A time zone only becomes involved when you format the date for that time zone. So, to display that date in some other time zone, just do that. Typically, this means supplying the desired time zone to your date formatter.

七度光 2024-09-22 19:37:51

我发现您返回的日期具有不同的时区 +200 而不是 -400。如果您注意到,您会发现该结果比您的原始日期晚了 12 小时(或 6 小时 + 6 小时)。当您确定新日期时,您是否也偶然更改了区域设置?当您进行更改并开始为您提供 +200 而不是 -400 时,系统会自动调整时间进行补偿,然后您又向该日期添加了 6 小时。 NSDate 将所有日期存储为自 2001 年 1 月 1 日 GMT 以来的时间间隔,并根据需要调整时区。

I see that the date you get back has a different timezone +200 not -400. If you notice you will see that that result is exactly 12 hours (or 6 hours + 6 hours) later than your original date. Did you by chance change the locale as well when you made the new date? When you made the change that started giving you +200 instead of -400 the system automatically adjusted the time to compensate, then you added another 6 hours to the date. NSDate stores all dates as a time interval since 1 Jan 2001 GMT and adjusts for timezones as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文