Categories
JavaScript Answers

How to parse date without time zone with JavaScript?

Spread the love

Sometimes, we want to parse date without time zone with JavaScript.

In this article, we’ll look at how to parse date without time zone with JavaScript.

How to parse date without time zone with JavaScript?

To parse date without time zone with JavaScript, we can add the time zone offset to the date without time zone.

For instance, we write

const date = new Date("2022-08-25T00:00:00");
const userTimezoneOffset = date.getTimezoneOffset() * 60000;
const d = new Date(date.getTime() - userTimezoneOffset);

to create a date object in UTC.

Then we call date.getTimezoneOffset to get the time zone offset in hours.

Then we multiply that by 60000 to get the offset in milliseconds.

Next, we call getTime to get the timestamp of date in milliseconds and then subtract that by userTimezoneOffset to get the date d that is in the local time zone.

Conclusion

To parse date without time zone with JavaScript, we can add the time zone offset to the date without time zone.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *