Last updated: Mar 5, 2024
Reading timeยท4 min
Use the setDate()
method to get the previous day of a date, e.g.
date.setDate(date.getDate() - 1)
.
The setDate
method takes the day of the month as a parameter and changes the
value for the Date
instance.
function getPreviousDay(date = new Date()) { const previous = new Date(date.getTime()); previous.setDate(date.getDate() - 1); return previous; } console.log(getPreviousDay()); // ๐๏ธ yesterday // ๐๏ธ Fri Dec 23 2022 console.log(getPreviousDay(new Date('2022-12-24'))); // ๐๏ธ Sat Dec 31 2022 console.log(getPreviousDay(new Date('2023-01-01')));
We created a reusable function that returns the previous day of a date.
We defined the previous
variable because the
Date.setDate() method mutates the Date
object it was called on.
This might not be what you want if you need to preserve the passed in Date
object.
Date
object that stores the same date and time.The setDate
method takes an integer between 1
and 31
that represents the
day of the month and changes the value on the given Date
object.
We used the getDate() method to get the day of the month of the specific date and then decremented the result by 1.
The getDate
method returns an integer between 1
and 31
.
getPreviousDay
function. If the user doesn't pass in a Date
object, the function returns yesterday's date.Note that the Date object automatically handles the scenario where the date is the first of the month.
If that is the case, the Date
object rolls back to the previous month
automatically.
function getPreviousDay(date = new Date()) { const previous = new Date(date.getTime()); previous.setDate(date.getDate() - 1); return previous; } // ๐๏ธ Tue Jan 31 2023 console.log(getPreviousDay(new Date('2023-02-01'))); // ๐๏ธ Sat Dec 31 2022 console.log(getPreviousDay(new Date('2023-01-01')));
The Date
object also handles the scenario where the previous day is the last
day of the previous year.
To get yesterday's date formatted as YYYY-MM-DD:
1
day from the current date to get yesterday's date.const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1); console.log(yesterday); // ๐๏ธ "Mon Jul 24 2023" function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-'); } console.log(formatDate(yesterday)); // ๐๏ธ "2023-07-24"
The Date() constructor returns the current date and time.
We used the Date.setDate() method to
change the day of the month on the Date
object.
setDate
method takes an integer 1-31
that represents the day of the month and changes the value on the given Date
object.We used the getDate method to get the day of the month for the specific date and then decremented the result by 1.
The getDate
method returns an integer between 1
and 31
.
Note that the Date object automatically handles the scenario where the date is the first of the month.
If that is the case, the Date
object rolls back to the previous month
automatically.
We defined a padTo2Digits
function that helps us handle the case where the
month or day of the month is less than 10
.
0
if they are less than 10
.The formatDate
function takes a Date
object as a parameter and returns a
string formatted as YYYY-MM-DD
.
The function makes use of the following 3 Date
related methods.
Date.getFullYear() method - returns a four-digit number representing the year that corresponds to a date.
Date.getMonth() - returns an
integer between 0
(January) and 11
(December) and represents the month for
a given date. Yes, unfortunately, the getMonth
method is off by 1
.
Date.getDate() - returns an
integer between 1
and 31
representing the day of the month for a specific
date.
getMonth
method returns a zero-based month index from 0 to 11, meaning January is 0
and December is 11
.Because the getMonth
method is zero-based we added 1 to its return value.
The last step is to place the calls to the methods in an array, so we can join them by a hyphen separator. Here are some examples.
console.log(['2023', '05', '24'].join('-')); // ๐๏ธ '2023-05-24' console.log(['2023', '09', '13'].join('-')); // ๐๏ธ '2023-09-13'
You could easily reorder the values in the array, change the separator to a
forward slash /
, or whatever suits your use case.
This formats yesterday's date as YYYY-MM-DD
. Here is the complete code
snippet.
const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1); console.log(yesterday); // ๐๏ธ Thu Jan 13 2022 function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-'); } console.log(formatDate(yesterday)); // ๐๏ธ "2022-01-13"
You can learn more about the related topics by checking out the following tutorials: