Right way to convert dates to UTC

大兔子大兔子 提交于 2019-12-11 01:20:14

问题


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.

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.

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.

Any help would be appreciated.

Thanks in advance,

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;
}

回答1:


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



来源:https://stackoverflow.com/questions/6124822/right-way-to-convert-dates-to-utc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!