I got some dates formatted as UTC.
These dates are actually timestamps of events happened locally around the world in the users local time and not real UTC. (It has been done like this due to MongoDB limitations of storing local time)
On the frontend, I want to use these fields without converting them into browsers local time.
Date string received from the backend looks like this:
2018-10-09T18:02:25.000Z
Creating a date object will convert it to browser's local time:
const date = new Date("2018-10-09T18:02:25.000Z")
console.log(date.getHours()) // Prints 20 since Im in +2 timezone
I want it to ignore the Zulu timezone information. I could remove the Z like this:
function toDateIgnoreUTC(dateString) {
return new Date(dateString.replace("Z", ""));
}
const date2 = toDateIgnoreUTC("2018-10-09T18:02:25.000Z")
date2.getHours() // Prints 18 which is what I want
I wonder if there's a better way of making the date object ignore the timezone information and not convert it to the browser local time.