Hello, I am giving a call to an weather API which returns me a JSON containing timezone -25200 and in the docs it is mentioned as “timezone: Shift in seconds from UTC” I want to convert this in actual date and time by using JavaScript.
That timezone is the number of seconds you must add to the UTC timestamp to get the local time. In your case it is equal to subtracting seven hours from the timestamp. (-25200 / 3600 = -7).
You seem to be in the North American Pacific Daylight time zone, known to computer programs everywhere as America/Los_Angeles or America/Vancouver.
Well, I am using below code to convert those milliseconds to actual time.
const dateBuilder = (timezone) => {
const nowInLocalTime = Date.now() + 1000 * (timezone / 3600);
const millitime = new Date(nowInLocalTime);
const dateFormat = millitime.toLocaleString();
let day = millitime.toLocaleString("en-US", {weekday: "long"});
let month = millitime.toLocaleString("en-US", {month: "long"});
let date = millitime.toLocaleString("en-US", {day: "numeric"});
let year = millitime.toLocaleString("en-US", {year: "numeric"});
let hours = millitime.toLocaleString("en-US", {hour: "numeric"});
let minutes = millitime.toLocaleString("en-US", {minute: "numeric"});
return `${day} ${date} ${month} ${year} ${hours}:${minutes}`;
}
And this code gives me time Saturday 26 December 2020 9 PM:24, for the location New York but it’s wrong, in New York now it’s 10:56AM, I also tried for the location London, but it gives me same time.