Last updated: Mar 6, 2024
Reading timeยท4 min
To add 1 day to a date:
getDate()
method to get the day of the month for the given date.setDate()
method to set the day of the month to the next day.setDate
method will add 1 day to the Date
object.function addOneDay(date = new Date()) { date.setDate(date.getDate() + 1); return date; } // โ Add 1 day to the current date const result1 = addOneDay(); console.log(result1); // ๐๏ธ 2023-07-26T07:05:46.110Z // ----------------------------------------------- // โ Add 1 day to a different date const date = new Date('2023-06-17T00:00:00.000Z'); const result2 = addOneDay(date); console.log(result2); // ๐๏ธ 2023-06-18T00:00:00.000Z
The addOneDay
function takes a Date
object as a parameter and adds 1 day to
the date.
Date
object to the function, the current date is used.If you need to add 1 day to the current date, call the Date()
constructor
without passing it any arguments.
// โ Add 1 day to the current date const date = new Date(); console.log(date); // ๐๏ธ 2023-07-25T07:06:57.576Z date.setDate(date.getDate() + 1); console.log(date); // ๐๏ธ 2023-07-26T07:06:57.576Z
We used the Date() constructor
to create a Date
object that represents the current date.
The Date.getDate() method
returns an integer between 1
and 31
that represents the day of the month for
the date.
const date = new Date(); console.log(date.getDate()); // ๐๏ธ 25
The
setDate()
method takes a number that represents the day of the month as a parameter and
sets the value on the Date
.
The JavaScript Date
object automatically takes care of rolling over the month
and/or year if adding a day to the date increments the month and year.
function addOneDay(date = new Date()) { date.setDate(date.getDate() + 1); return date; } // โ Add 1 day to a different date const date = new Date('2023-02-28T00:00:00.000Z'); const result = addOneDay(date); console.log(result); // ๐๏ธ 2023-03-01T00:00:00.000Z
The example shows how creating a date for the 28th of February 2023 and adding
1
day to the result rolls the month over to March.
Note that the setDate
method mutates the Date
object it was called on.
If you don't want to change the Date
in place, create a copy of it before
calling the method.
function addOneDay(date = new Date()) { const dateCopy = new Date(date); dateCopy.setDate(dateCopy.getDate() + 1); return dateCopy; } const date = new Date('2023-02-17T00:00:00.000Z'); const result = addOneDay(date); console.log(result); // ๐๏ธ 2023-02-18T00:00:00.000Z console.log(date); // ๐๏ธ 2023-02-17T00:00:00.000Z
When a Date
object is passed to the Date()
constructor, it gets converted to
a timestamp and can be used to create a copy of the date.
Mutating function parameters is a bad practice because calling the function with the same parameter multiple times returns different results.
Instead, pure functions like the one above return the same output when called with the same parameters.
You can also use date-fns module to add 1 day to a date.
import {addDays} from 'date-fns'; // โ Add 1 day to the current Date const result1 = addDays(new Date(), 1); console.log(result1); // ๐๏ธ 2023-07-26T07:10:32.724Z // โ Add 1 day to a different date const date = new Date('2023-02-17T00:00:00.000Z'); const result2 = addDays(date, 1); console.log(result2); // ๐๏ธ 2023-02-18T00:00:00.000Z console.log(date); // ๐๏ธ 2023-02-17T00:00:00.000Z
The addDays()
function takes a date and the number of days to be added to the
date as parameters.
The function doesn't mutate the original date as shown in the example.
If you don't have date-fns
installed, you can install it by running the
following command from your terminal.
# ๐๏ธ create package.json if you don't have one npm init -y # โ install with NPM npm install date-fns # โ install with YARN yarn add date-fns
You can also use the moment.js module to add 1 day to a date.
import moment from 'moment'; const result1 = moment(new Date()).add(1, 'days'); console.log(result1); // ๐๏ธ 2023-01-15T07:41:46.845Z const date = new Date('2023-02-17T00:00:00.000Z'); const result2 = moment(date).add(1, 'days'); console.log(result2); // ๐๏ธ 2023-02-18T00:00:00.000Z console.log(date); // ๐๏ธ 2023-02-17T00:00:00.000Z
We used the moment().add()
method to add 1 day to a date.
The method can be used to add years, quarters, months, weeks, days, hours, minutes, seconds or milliseconds to a date.
If you don't have moment
installed, you can install it by running the
following command from your terminal.
# ๐๏ธ create package.json if you don't have one npm init -y # โ install with NPM npm install moment # โ install with YARN yarn add moment
The call to the add()
method actually returns a moment
object and not a
native JavaScript date.
If you need to convert the value to a JavaScript date, use the toDate()
method.
import moment from 'moment'; const result1 = moment(new Date()).add(1, 'days').toDate(); console.log(result1); // ๐๏ธ 2023-01-15T07:41:46.845Z const date = new Date('2023-02-17T00:00:00.000Z'); const result2 = moment(date).add(1, 'days').toDate(); console.log(result2); // ๐๏ธ 2023-02-18T00:00:00.000Z console.log(date); // ๐๏ธ 2023-02-17T00:00:00.000Z
The toDate()
method takes care of converting the moment
object to a native
JavaScript Date object.
You can learn more about the related topics by checking out the following tutorials: