JavaScript Subtract Days from the Date
When we work with dates, it is essential to know how to perform operations like subtracting days from a date. So, in this article, we will see how to subtract days from dates in JavaScript.
What is subtracting days from the date?
It refers to the process of reducing the value of the day component of a given date by a specific number of days. This allows you to calculate a new date that is a certain number of days earlier than the original date.
Why should we subtract days from the date?
- When we work with schedules or calendars, it can help determine previous data based on a given reference point.
- If you need to display the remaining days until a specific event, subtracting the current date from the target date will give you the number of days left.
- If you want to calculate a past or future date based on a given reference date, this allow you to adjust the date accordingly.
2 methods to subtract days from the date
Follow these two methods to subtract days from dates easily.
Method 1: Using the Date Object
We can use a date object, which offers a set of built-in methods to handle dates to subtract days from dates.
Step 1: Create a new Date object, specifying the original date.
let originalDate = new Date();
Here, we have created the new Date() object without passing any arguments to the variable “originalDate”. If we don’t pass any arguments, it returns the current date and time.
Step 2: Subtract the desired number of days from the original date.
We can use the setDate() method to modify the day of the month. To subtract days, simply pass a negative value as an argument, like:
originalDate.setDate(originalDate.getDate() - 5);
Here, we pass the negative value 5.
Step 3: Retrieve the updated date.
We can obtain the updated date in a human-readable format by using the toDateString() method.
let updatedDate = originalDate.toDateString();
console.log(updatedDate);
So, It will display the updated date based on the original date minus the specified number of days.
Method 2: Using the Moment.js Library
We can also use the Moment.js library, which is one of the popular javascript libraries to subtract days from date. It also provides extensive functionalities for handling and manipulating dates.
Step 1: Link Moment.js library CDN in your HTML file
You can easily link the moment.js library by pasting the given below script to your HTML file.
<script src="https://cdn.jsdelivr.net/momentjs/2.29.1/moment.min.js"></script>
Step 2: Create a moment object with the original date
let originalDate = moment('2023-05-22');
Here, we create the object of moment.js with the original date and stored it in the variable ‘originalDate’
Step 3: Subtract days from the date using the subtract() method
originalDate.subtract(5, 'days');
Here, we subtracted 5 days from the variable ‘originalDate’ by using subtract() method.
Step 4: Retrieve the updated date
let updatedDate = originalDate.format('YYYY-MM-DD');
console.log(updatedDate);
In the above code, we can specify the format ‘YYYY-MM-DD’ for the updated date using the format() method. So, it will return the updated date based on the original date minus the specified number of days.
Conclusion
You can enhance your applications with various date-related functionalities by knowing how to subtract days from a date.