How to subtract days from a date in JavaScript

Subtracting days from dates is essential for calculating deadlines, creating date ranges, and implementing scheduling features in web applications. As the creator of CoreUI, a widely used open-source UI library, I’ve implemented date subtraction in numerous dashboard components, reports, and calendar widgets. From my expertise, the most reliable solution is to use the setDate() method combined with getDate() to modify the date value. This approach automatically handles month boundaries, leap years, and other calendar complexities.

Use setDate() with getDate() minus the desired number of days to subtract days from a date.

const date = new Date()
date.setDate(date.getDate() - 7)

The getDate() method returns the day of the month (1-31), and setDate() sets a new day value. When you subtract days using date.getDate() - 7, JavaScript automatically handles month and year boundaries. For example, if today is March 3rd and you subtract 7 days, the result will correctly be February 24th (or 25th in leap years). This approach works seamlessly across month and year boundaries without additional calculations.

Best Practice Note:

This is the same approach we use in CoreUI components for date range pickers and scheduling features. To avoid mutating the original date object, create a new Date instance: new Date(originalDate.getTime() - (days * 24 * 60 * 60 * 1000)).


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author