How to get yesterday's date using JavaScript

How do you use JavaScript to get yesterday's date?

Well, first you want to get the date of the current time (today), and then subtract one day from it:

const today = new Date()
const yesterday = new Date(today)

yesterday.setDate(yesterday.getDate() - 1)

today.toDateString() yesterday.toDateString()

We usesetDate()Method openyesterday, Pass the current date minus 1 as a parameter.

Even on the first day of each month, JavaScript is logical enough, and it will point to the last day of the previous month.

Download mine for freeJavaScript beginner's manual


More js tutorials: