Categories
JavaScript Answers

How to fix incorrect date shown in new Date() in JavaScript?

Spread the love

Sometimes, we want to fix incorrect date shown in new Date() in JavaScript.

In this article, we’ll look at how to fix incorrect date shown in new Date() in JavaScript.

How to fix incorrect date shown in new Date() in JavaScript?

To fix incorrect date shown in new Date() in JavaScript, we should include the time in the date string when we use it as an argument for Date.

For instance, we write:

const d = new Date('2022-09-05 00:00')
console.log(d)

to create date d with '2022-09-05 00:00' as the argument of the Date constructor.

As a result, d is Mon Sep 05 2022 00:00:00 GMT-0700 (Pacific Daylight Time).

Conclusion

To fix incorrect date shown in new Date() in JavaScript, we should include the time in the date string when we use it as an argument for Date.

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 *