将日期转换为UTC的正确方法 [英] Right way to convert dates to UTC

查看:146
本文介绍了将日期转换为UTC的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这个问题没有多大意义,我深表歉意,但是我是ActionScript的新手,想知道您是否可以帮助我解决这个问题。

my apologies if this question doesn't make much sense, but I'm new(ish) to Actionscript, and was wondering if you could help me solving this problem.

我正在构建一个给定时区的应用程序,它告诉您该位置的时间。当我在时区为格林尼治标准时间(UTC)的位置工作时,它会很好用,但如果主机上的时区为其他任何时区,都会惨不忍睹,因为无论如何它仍然会增加主机的时间。

I'm building an application that given a time-zone, tells you what time in that location it is. It works great when I'm working with locations where the timezone is GMT (UTC), but fauils miserably if the timezone on the host machine is anything else, as it will still add the time to the host machine regardless.

所以我在想我要做的是将机器时间转换为UTC(使用getUTCDate()等),并将其用作基础。我想知道这是否是正确的方法,或者是否有更好的方法。

So I was thinking what I have to do, is convert the machine time, into UTC (using getUTCDate() etc), and use that as a base. I was wondering if that's the right way to go, or if there's any better way to do it.

任何帮助将不胜感激。

预先感谢

更新
在Eugeny89回答之后,我修改了代码并创建了此方法为我将当前日期转换为UTC。只是想知道这是否是最好的方法。

UPDATE After Eugeny89's answer, I modified my code and created this method to convert the current date to UTC for me. Just wondering if that's the best way to go about it.

private function convertToUTC(dtDate:Date):Date{
            dtDate.setTime(dtDate.getTime() + (dtDate.getTimezoneOffset() * 60000))
            return dtDate;
}


推荐答案

使用 getTimezoneOffset()(用户时间与UTC时间之间的分钟数差异)或 Date类的 getUTCHours() 。有关更多详细信息,请参见手册

Use getTimezoneOffset() (difference in minutes between users time and UTC time) or getUTCHours() of class Date. See manual for more details

这篇关于将日期转换为UTC的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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