. Advertisement .
..3..
. Advertisement .
..4..
JavaScript is based on codes, and programmers know the importance of the language. Being a scripting language, programmers use it to enhance HTML and embed it in HTML code. To make a web page interactive and dynamic, JavaScript is used as it allows to take variable text, create cookies, detect a user’s browser, etc.
Beginners who are just starting up with JavaScript, face errors, issues, coding assistance, and “Subtract Days from a Date in JavaScript” is one of those issues that we are going to discuss today to help you code it efficiently.
How To Subtract Days from a Date in JavaScript?
Subtracting days from a date is a programming operation that you may have to do quite often in JavaScript. The two codes that are used to subtract days are getDate()
and setDate()
.
The getDate
method gets the day of the Date. You can subtract the number of days with this method. To set the date, call the setDate()
method.
Check out the tutorial to look at the code
Using getDate()
const date = new Date("05/28/2021"); // 28th May 2021
console.log(date.getDate()); // 28
Using setDate()
const date = new Date("05/28/2021"); // 28th May 2021
// Set the day to 10th
date.setDate(10);
console.log(date); // 10th May 2021
With the combination of these two methods, days can be subtracted from the date
const date = new Date("05/28/2021"); // 28th May 2021
// Subtract 7 days from the Date object
date.setDate(date.getDate() - 7);
console.log(date); // 21st May 2021
The setDate()
adjusts the month and year of the date automatically forward and backward.
With the following code, you can subtract the days back to April.
const date = new Date("05/28/2021"); // 28th May 2021
// Subtract 35 days from the Date object
date.setDate(date.getDate() - 35);
console.log(date); // 23rd April 2021
And that’s how you can subtract days from a date using JavaScript.
Without Daylight Saving
The time is advanced if we look at the daylight saving time between March and November. In order to ignore Daylight saving time and calculate the difference, use Date.UTC()
method to convert the date. For this, you have to pass the day, month, and year to this method.
Look at the example
const date1 = new Date("05/28/2021"); // 28th May 2021
const date2 = new Date("04/17/2021"); // 17th April 2021
const date1UTC = Date.UTC(
date1.getFullYear(),
date1.getMonth(),
date1.getDate()
);
const date2UTC = Date.UTC(
date2.getFullYear(),
date2.getMonth(),
date2.getDate()
);
const difference = date1UTC - date2UTC;
console.log(difference / (1000 * 60 * 60 * 24)); // 41
with this code, you can efficiently ignore the Daylight saving time while calculating.
Conclusion
And that’s it!
Here, you have learned how to subtract days from a date in JavaScript. I believe no coding is difficult or impossible to crack if you get the right tips and tricks to it through.
Being a beginner, it is really important to learn the basics of coding. With that note, I am hoping you get the information with this tutorial you came here for.
Feel free to drop a comment below!
Leave a comment