DEV Community

Cover image for Subtract days from a date in JavaScript without a library
Reza Lavarian
Reza Lavarian

Posted on • Originally published at decodingweb.dev

Subtract days from a date in JavaScript without a library

Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

How do you subtract days from a date in JavaScript?

To subtract a few days from a date in JavaScript, you need to use setDate() on a Date object and decrement the current day of the month – returned by getDate() – by one or more days.

You can do it in three steps:

  1. Get the day of the month by the Date.prototype.getDate() method
  2. Take one or more days from the getDate() returned value
  3. Use Date.prototype.setDate() to update the object

For instance, to get the date 5 days ago in JavaScript:

// Get the current date
const currentDate = new Date()

// Instantiate another date object to avoid mutating the current date object
const pastDate = new Date(currentDate)
pastDate.setDate(pastDate.getDate() - 5)

console.log(futureDate)
Enter fullscreen mode Exit fullscreen mode

In the above example, we instantiate the pastDate off the currentDate for two reasons:

  1. To avoid mutating the original date object (currentDate in this case)
  2. To guarantee we subtract the days from a date object identical to the original

The getDate() method returns the day of the month of the date object on which it's called. The return value is an integer number between 1 and 31.

Next, we subtract 5 from the value returned by getDate() - and pass it to setDate() to save the result.

The Date object works smartly when modifying the days. If the result is outside the acceptable range for the respective month, setDate() will update the Date object accordingly.

For instance, if it's November 1st, and we take 5 days from it, the result would be October 27th!

Create a helper function to add days to a specific date

To make this functionality reusable, you can create a helper function that accepts a date and an arbitrary number of days to subtract.

function subtractDaysFromDate(currentDate, daysToSubtract) {
    daysToSubtract = daysToSubtract || 0

    // Instantiate a new object based on the current Date
    const pastDate = new Date(currentDate)

    // Subtract  the number of days
    pastDate.setDate(pastDate.getDate() - daysToSubtract)

    return pastDate
}
Enter fullscreen mode Exit fullscreen mode

So to subtract 7 days from a date object:

console.log(subtractDaysFromDate(new Date('2022-11-05'), 7))
// expected output: Sat Oct 29 2022 00:00:00 GMT+0100 (Western European Summer Time)
Enter fullscreen mode Exit fullscreen mode

You can also add days to specific date using the same logic. For instance, 5 days from today. All you need to do is to replace the - operator with the + operator.

And that's how you can subtract days from a date in JavaScript without a library. I hope you found this guide helpful.

Thanks for reading!


❤️ You might like:

Top comments (1)

Collapse
 
naucode profile image
Al - Naucode

Great article, you got my follow, keep writing!