Categories
JavaScript Answers

How to get the previous day with JavaScript?

Spread the love

Sometimes, we want to get the previous day with JavaScript.

In this article, we’ll look at how to get the previous day with JavaScript.

How to get the previous day with JavaScript?

To get the previous day with JavaScript, we can call setDate with the date minus 1.

For instance, we write:

const dateObj = new Date()
dateObj.setDate(dateObj.getDate() - 1);
console.log(dateObj)

We create a new Date instance and assigned it to dateObj.

Then we set the dateObj to the previous day by calling setDate with dateObj.getDate() - 1.

Conclusion

To get the previous day with JavaScript, we can call setDate with the date minus 1.

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 *