简体   繁体   中英

Convert Unix time to other GMT in java script

I'm a newbie in Javascript, i have a problem when convert UNIX time to other GMT time. My code below:

This function to get Unix time from server

            fetch(url)
            .then(
                function (response) {
                    if (response.status !== 200) {
                        console.log('Looks like there was a problem. Status Code: ' +
                            response.status);
                        return;
                    }                   
                    response.json().then(function (data) 
                    {
                        //data is Unixtime , data=1596514540815
                        //I want convert it to other GMT date time, example GMT+2, GMT+3...
                        
                    });
                }
            )
            .catch(function (err) {
                console.log('Fetch Error :-S', err);
            });

I want convert it (data) to other GMT date time,example GMT+1,GMT+2... How can i do it?

First create the date and then get a string for the timezone you want:

var date = new Date(1596514540815);

console.log(date.toLocaleString("en-US", {timeZone: "Australia/Brisbane"}));
console.log(date.toLocaleString("en-US", {timeZone: "Asia/Shanghai"}));
console.log(date.toLocaleString("en-US", {timeZone: "America/New_York"}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM