1

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 ?

ledien
  • 529
  • 2
  • 8
  • 19
  • 1
    Perhaps this question: https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript – Mark Taylor Aug 04 '20 at 04:33

1 Answers1

2

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"}));
Andrew Arthur
  • 1,563
  • 2
  • 11
  • 17