In JavaScript, there are many built-in methods that we can work with each element of the ‘Date’ object. To subtract days from a Date in JavaScript, we can also use setDate()
, getTime()
, and getDate()
methods to do it. Let me clarify it in the next part of this article.
How did I subtract days from a Date in JavaScript?
Using the getTime() method
getTime() method will return the total time since January 1, 1970 00:00:00 in milliseconds. In this tutorial, we will use it to subtract days from a Date in Javascript.
Syntax
Date.getTime()
Parameter
- NONE
Example:
const date = new Date(); let timeOfDay = 60 * 60 * 24 * 1000; // Calculate milliseconds in a year // Subtract days from a Date using the getTime() method let result = new Date(date.getTime() - 3 * timeOfDay); console.log(result);
Output
Thu Dec 15 2022 11:23:35 GMT+0700 (Indochina Time)
Above is the code. I used the getTime()
method to subtract days from a Date by taking the result of the getTime()
method and then subtracting the amount of time you want to subtract later in this example which is three days, then get the result. This result creates a new date, the date we want to get after subtracting.
Using the setDate() and getDate() method
The setDate()
and getDate() methods help us get and change the date we use to call the above two methods. We can use these two methods to subtract days from a Date very simply.
setDate() method
setDate(dayValue)
Parameter
- dayValue: The date value that you want to change
If you pass in a number less than zero or greater than the number of tusks for that month, the month of that date will change, plus or minus.
Example:
const date = new Date(); console.log(date); date.setDate(-1); console.log(date); date.setDate(35); console.log(date);
Output
Sun Dec 18 2022 11:21:24 GMT+0700 (Indochina Time)
Tue Nov 29 2022 11:21:24 GMT+0700 (Indochina Time)
Mon Dec 05 2022 11:21:24 GMT+0700 (Indochina Time)
getDate() method
Syntax
Date.getDate()
Parameter
- NONE
This method is used to get the date of the created date object.
After learning about the two methods, getDate()
and setDate()
, we will use them to subtract days from a Date as follows:
const date = new Date(); // Subtract days from a Date using the setDate() and getDate() method date.setDate(date.getDate() - 3); console.log(date);
Output
Thu Dec 15 2022 11:20:38 GMT+0700 (Indochina Time)
We see that we changed the date we wanted to subtract to 3 days earlier but changed the original date.
In some cases, we want to get the previous dates but keep that date the same to retrieve or use for something. So I would perform fetching the previous dates without changing the original date as follows:
Example:
const date = new Date(); // Subtract days from a Date using the setDate() and getDate() method function subtractDays(numOfDaysBack, date) { let dateResult = new Date(date.toDateString()); dateResult.setDate(dateResult.getDate() - numOfDaysBack); return dateResult; } console.log(subtractDays(3, date)); console.log(date);
Output
Thu Dec 15 2022 00:00:00 GMT+0700 (Indochina Time)
Sun Dec 18 2022 11:19:37 GMT+0700 (Indochina Time)
You can check that the date result of the ‘date’ has not changed. We can use the date difference to subtract days from a Date.
Summary
Above are the two ways I subtract days from a Date in JavaScript. I use the getTime() method because it’s pretty concise and convenient. You can try both ways. This will help you understand it better and remember it longer. I hope this article helps you and your program. Good luck.
Maybe you are interested:
- Subtract Minutes from a Date in JavaScript
- Subtract Months from a Date in JavaScript
- Subtract Years from a Date in JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css