How to get yesterdays date in JavaScript

How to get yesterdays date in JavaScript image

In this post we cover how to get yesterdays date in JavaScript

To get yesterdays date in JavaScript, todays date can be subtracted by 1 which will provide yesterdays date.

Here is an example:

1const today = new Date();
2const yesterday = new Date();
3yesterday.setDate(today.getDate() - 1);
4
5console.log(yesterday); //Mon May 29 2023 08:22:07 GMT+0100 (British Summer Time)

JavaScript

JavaScript is a programming language that is primarily used to help build frontend applications.

It has many uses, such as making network requests, making dynamic or interactive pages, creating business logic and more.

JavaScript Date Object

The JavaScript Date Object is used to capture a moment in time and to be able to use this time programatically.

Further resources

Related topics

Save code?

How to get yesterdays date in JavaScript code example image

© 2024 MWXYZ