I'm trying to host an app with a countdown timer that subtracts the current UTC date and time from a given UTC date and time in the future. But the issue is with new Date()
, new Date(Date.now())
, etc I keep getting local time which is several hours off. How can I create a new Date object with the current time in UTC?
let date = new Date()
date = date.toUTCString().slice(0, date.toUTCString().length - 13);
let time = "17:00:00"
// this is the UTC time of the event, this works hosted
// locally this has to be "12:00:00"
const timeLeft = new Date(`${date} ${time}`) - new Date();
const hoursLeft = Math.floor((timeLeft / (1000 * 60 * 60)));
const minutesLeft = Math.ceil((timeLeft / 1000) / 60 % 60);
// I'm trying to get a consistent hoursLeft on both local and hosted machines
I'm probably going to just use moment.js to avoid the issue and simplify the code, but I'd still really like to know an answer.