📋 Table Of Content
So the approach is get the current day date and then subtract one day from the it and set the yesterday date.
const today = new Date() const yesterday = new Date(today) yesterday.setDate(yesterday.getDate() - 1) const yesterdayDate = yesterday.toDateString() console.log(yesterdayDate)
Tue Nov 09 2021
new Date() : It give the current date and time of the day.
yesterday = new Date(today) : added the date on yesterday variable to change the day to yesterday's using setDate().
setDate() : It lets us to change the day of the month at a given instance.
getDate() : This method extract the date of the month from the string returned by new Date(). Example: 2021-11-10T17:56:48 will return 10.
toDateString() : This method returns the date in a proper format in English. Example, from 2021-11-10T17:56:48 to Tue Nov 09 2021
Solution 2 : Using momentJs
You can just type
moment().format("YYYY-MM-DD") to get the the current date with the correct format.
Current Date in Different Date Formats (mm-dd-yyyy)