Get time of specific timezone [duplicate]

2019-01-03 02:09发布

This question already has an answer here:

I am using a JavaScript Date class & trying to get the current date using getDate() method. But obviously it is loading system date & time. I am running the code from India but I want to get the date & time of UK using the same method. How can I do that ?

9条回答
放我归山
2楼-- · 2019-01-03 02:40

This is Correct way to get ##

function getTime(offset)
        {
            var d = new Date();
            localTime = d.getTime();
            localOffset = d.getTimezoneOffset() * 60000;

            // obtain UTC time in msec
            utc = localTime + localOffset;
            // create new Date object for different city
            // using supplied offset
            var nd = new Date(utc + (3600000*offset));
            //nd = 3600000 + nd;
            utc = new Date(utc);
            // return time as a string
            $("#local").html(nd.toLocaleString());
            $("#utc").html(utc.toLocaleString());
        }
查看更多
祖国的老花朵
3楼-- · 2019-01-03 02:43

You could use Intl.DateTimeFormat.

var options = {
    timeZone: 'Europe/London',
    year: 'numeric', month: 'numeric', day: 'numeric',
    hour: 'numeric', minute: 'numeric', second: 'numeric',
},
    formatter = new Intl.DateTimeFormat([], options)
formatter.format(new Date())

Alternatively, if you're formatting just once instead of bulk use Date.prototype.toLocaleDateString().

(new Date()).toLocaleString([], options)

Unfortunately browsers are not required to understand timezones other than UTC, so try these blocks and figure out an alternative in case it fails, for example fetch the timezone offset from a server.

查看更多
爷、活的狠高调
4楼-- · 2019-01-03 02:45

short answer from client-side: NO, you have to get it from the server side.

查看更多
Ridiculous、
5楼-- · 2019-01-03 02:49

If it's really important that you have the correct date and time; it's best to have a service on your server (which you of course have running in UTC) that returns the time. You can then create a new Date on the client and compare the values and if necessary adjust all dates with the offset from the server time.

Why do this? I've had bug reports that was hard to reproduce because I could not find the error messages in the server log, until I noticed that the bug report was mailed two days after I'd received it. You can probably trust the browser to correctly handle time-zone conversion when being sent a UTC timestamp, but you obviously cannot trust the users to have their system clock correctly set. (If the users have their timezone incorrectly set too, there is not really any solution; other than printing the current server time in "local" time)

查看更多
贪生不怕死
6楼-- · 2019-01-03 02:50

You can use getUTCDate() and the related getUTC...() methods to access a time based off UTC time, and then convert.

If you wish, you can use valueOf(), which returns the number of seconds, in UTC, since the Unix epoch, and work with that, but it's likely going to be much more involved.

查看更多
我命由我不由天
7楼-- · 2019-01-03 02:53

The best way to do this is to use getLocaleString, like this:

Create a date object:

date = new Date(0)

If you are in Berlin, this should convert to this string:

Thu Jan 01 1970 01:00:00 GMT+0100 (CET)

Get the hours in Athens:

date.toLocaleString('de-DE', {hour: '2-digit',   hour12: false, timeZone: 'Europe/Athens' })

'02'

Get the hours in Shanghai:

date.toLocaleString('de-DE', {hour: '2-digit',   hour12: false, timeZone: 'Asia/Shanghai' })

'08'

查看更多
登录 后发表回答