AS3 Date对象不返回时间正确 [英] As3 Date Object Not Returning Hours Correctly

查看:652
本文介绍了AS3 Date对象不返回时间正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个非常简单的,但恼人的问题。我创建一个Date对象,并调用getHours()方法返回的似乎永远是零。也许我做错了什么?

So I have a really simple, yet annoying problem. I am creating a date object and calling the getHours() method and the return seems to always be zero. Perhaps I am doing something wrong?

var d:Date = new Date(1382166000000); // 10 / 19 / 13 @ 2:00:00am EST
trace(d.getTime() + " : " + d.getHours());

//output: 1382166000000 : 0

任何想法?除非我身世困惑不应getHours在返回2?

Any ideas? Unless I am monumentally confused shouldn't getHours be returning 2?

推荐答案

日期不工作,你觉得它的方式。它可以存在两种格式之一:本地时间或UTC

Date doesn't work the way you think it does. It can exist in one of two formats: the local time or UTC.

getHours()方法将返回本地时间,或根据计算机的内部时区偏移格式的UTC时间。你传递的时间实际上是上午07点在UTC。因此,通过这种逻辑,你(和我)都在PST(或UTC-7)。如果您运行 d.timezoneOffset ,它应该返回 420 ,这是7小时分钟。

The getHours() method returns local time, or the UTC time formatted according to the computer's internal timezone offset. The time you passed is actually 7AM in UTC. So by that logic, you (and I) are in PST (or UTC-7). If you run d.timezoneOffset, it should return 420, which is 7 hours in minutes.

现在,大部分时间您从来没有想用任何东西,除了UTC工作(UTC是一个常数,特别是在计算机)。所以忘了 getHours()方法,看看 getUTCHours()代替。

Now, most of the time you never want to work with anything except UTC (UTC is a constant, especially in computers). So forget the getHours() method and look at getUTCHours() instead.

trace(d.getTime() + " : " + d.getUTCHours ()); // output 1382166000000 : 7

所以,这将使你在UTC个小​​时的时间。接下来,你要确定你希望把它格式化为哪个时区。 EST为UTC-5,所以你要从UTC时间减去5。

So that will give you the time in UTC hours. Next, you have to determine what time zone you want to format it as. EST is UTC-5, so you want to subtract 5 from the UTC hours.

trace(d.getTime() + " : " + ( d.getUTCHours () - 5 )); // output 1382166000000 : 2

和,让你正是你所期待的。

And that gives you exactly what you were expecting.

只是重申:AS3的日期类的<​​strong>不允许设置时区。你只需要进入到UTC和本地时区的时间。而已。如果你想一次在任何其它时区,你需要手动进行格式化,因为我做上面。

Just to reiterate: AS3's Date class does not allow for setting the timezone. You only have access to the time in UTC and in the local time zone. Nothing more. If you want time in any other time zone, you need to format it manually, as I did above.

这篇关于AS3 Date对象不返回时间正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆