Categories
JavaScript Answers

How to get yesterday’s date with moment.js and JavaScript?

Spread the love

Sometimes, we want to get yesterday’s date with moment.js and JavaScript.

In this article, we’ll look at how to get yesterday’s date with moment.js and JavaScript.

How to get yesterday’s date with moment.js and JavaScript?

To get yesterday’s date with moment.js and JavaScript, we call the subtract method.

For instance, we write

const yesterday = moment().subtract(1, "days");

to call moment to create a moment object with the current date.

And then we call subtract with 1 and 'days' to return a moment object that subtracts the current date by 1 day.

Conclusion

To get yesterday’s date with moment.js and JavaScript, we call the subtract method.

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 *