How to Add 1 Day to a Date using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
4 min

banner

# Add 1 Day to a Date using JavaScript

To add 1 day to a date:

  1. Use the getDate() method to get the day of the month for the given date.
  2. Use the setDate() method to set the day of the month to the next day.
  3. The setDate method will add 1 day to the Date object.
index.js
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

add 1 day to date

The code for this article is available on GitHub

The addOneDay function takes a Date object as a parameter and adds 1 day to the date.

If you don't pass a 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.

index.js
// โœ… 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.

index.js
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.

index.js
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

javascript date object rolls over month automatically

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.

# Add 1 day to a Date without mutation

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.

index.js
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

add 1 day to date without mutation

The code for this article is available on GitHub

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.

# Add 1 Day to a Date using date-fns

You can also use date-fns module to add 1 day to a date.

index.js
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

add 1 day to date using date fns

The code for this article is available on GitHub

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.

shell
# ๐Ÿ‘‡๏ธ 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

# Add 1 Day to a Date using moment.js

You can also use the moment.js module to add 1 day to a date.

index.js
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
The code for this article is available on GitHub

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.

shell
# ๐Ÿ‘‡๏ธ 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.

index.js
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev