Right way to convert dates to UTC

2019-07-24 18:30发布

问题:

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