How to Subtract Days from a Date in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
4 min

banner

# Subtract Days from a Date in JavaScript

To subtract days from a date:

  1. Use the getDate() method to get the day of the month for the specific date.
  2. Use the setDate() method to set the day of the month for the date.
  3. The setDate method takes the day of the month as a parameter and sets the value for the date.
index.js
function subtractDays(date, days) { date.setDate(date.getDate() - days); return date; } // โœ… Subtract 1 day from the current Date const result1 = subtractDays(new Date(), 1); console.log(result1); // ๐Ÿ‘‰๏ธ 2023-07-28T03:30:37.511Z // โœ… Subtract 2 days from a different date const date = new Date('2024-03-18T06:30:10.000Z'); const result2 = subtractDays(date, 2); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-16T06:30:10.000Z

subtract days from date

The code for this article is available on GitHub

The subtractDays() function takes a Date object and N as parameters and subtracts N days from the date.

If you need to subtract days from the current date, call the Date() constructor without passing it any arguments.

index.js
function subtractDays(date, days) { date.setDate(date.getDate() - days); return date; } const currentDate = new Date(); // โœ… Subtract 1 day from the current date const result = subtractDays(currentDate, 1); console.log(result); // ๐Ÿ‘‰๏ธ 2023-07-28T03:31:53.929Z

subtract days from 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 currentDate = new Date(); console.log(currentDate.getDate()); // ๐Ÿ‘‰๏ธ 13

The Date.setDate() method takes a number that represents the day of the month as a parameter and sets the value on the Date.

If a negative number is provided to the setDate method, the date will be set counting backward from the last day of the previous month.

In other words, the JavaScript Date object automatically takes care of adjusting the month and year if subtracting X days from the date pushes us into the previous month or year.

index.js
function subtractDays(date, days) { date.setDate(date.getDate() - days); return date; } const date = new Date('2024-03-18T06:30:10.000Z'); const result = subtractDays(date, 20); console.log(result); // ๐Ÿ‘‰๏ธ 2024-02-27T06:30:10.000Z

Subtracting 20 days from the date automatically adjusted the month in the example.

# Get a date of X days and reset it to Midnight

If you also want to reset the time component of the date to midnight, you can use the setHours() method.

index.js
function getDateXDaysAgo(numOfDays, date = new Date()) { const daysAgo = new Date(date.getTime()); daysAgo.setDate(date.getDate() - numOfDays); daysAgo.setHours(0, 0, 0, 0); return daysAgo; } const date = new Date('2022-01-17'); // ๐Ÿ‘‡๏ธ Fri Dec 31 2021 00:00:00 console.log(getDateXDaysAgo(17, date));

get date of x days and reset it to midnight

The code for this article is available on GitHub

The 4 parameters we passed to the setHours() method are: hours, minutes, seconds and milliseconds.

This has the effect of resetting the time component of the Date to midnight.

# Subtract Days from 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 before calling the method.

index.js
function subtractDays(date, days) { const dateCopy = new Date(date); dateCopy.setDate(dateCopy.getDate() - days); return dateCopy; } const date = new Date('2024-03-18T06:30:10.000Z'); const result = subtractDays(date, 6); console.log(result); // ๐Ÿ‘‰๏ธ 2024-03-12T06:30:10.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-18T06:30:10.000Z

subtract days from 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.

# Subtract Days from a Date using date-fns

You can also use date-fns module to subtract days from a date.

index.js
import {subDays} from 'date-fns'; const date = new Date('2024-03-18T06:30:10.000Z'); const result1 = subDays(date, 6); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-12T06:30:10.000Z const result2 = subDays(date, 10); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-08T06:30:10.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-18T06:30:10.000Z

subtract days from date using date fns

The code for this article is available on GitHub

The subDays() function takes a date and the number of days to be subtracted from 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

# Subtract Days from a Date using moment.js

You can also use the moment.js module to subtract days from a date.

index.js
import moment from 'moment'; const date = new Date('2024-03-18T06:30:10.000Z'); const result1 = moment(date).subtract(6, 'days'); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-12T06:30:10.000Z const result2 = moment(date).subtract(10, 'days'); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-08T06:30:10.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-18T06:30:10.000Z

subtract days from date using date fns

The code for this article is available on GitHub

We used the moment().subtract() method to subtract days from a date.

The method can be used to subtract years, quarters, months, weeks, days, hours, minutes, seconds or milliseconds from 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 date = new Date('2024-03-18T06:30:10.000Z'); const result1 = moment(date).subtract(6, 'days').toDate(); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-12T06:30:10.000Z const result2 = moment(date).subtract(10, 'days').toDate(); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-08T06:30:10.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-18T06:30:10.000Z
The code for this article is available on GitHub

The toDate() method takes care of converting the moment object to a native JavaScript Date object.

I've also written an article on how to add days to a Date.

# 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