Add Days to Date in JavaScript

A common requirement while working with dates in JavaScript is to add days to a date. Let us see how to do this in JavaScript

JavaScript provides the Date object for manipulating date and time. Amongst the various methods of the Date object, the one will be focusing in this post are the setDate(value) and getDate() which sets and gets the day of the month respectively.

Add Days to Date in JavaScript

<head>
<
title>Add Days to a Date from DevCurry.com</title>
<
script type="text/javascript">
var newDt = new Date();
document.writeln("Current Date :" + newDt + "<br/>");
// add 5 days to the
current date
newDt.setDate(newDt.getDate() + 5);
document.writeln("New Date :" + newDt);
</script>
</
head>

Add Days to Date

Similarly to add months, JavaScript has the setMonth(month,date) and getMonth() method and to add Years, you can use the setFullYear() and getFullYear() methods.

No comments:

Post a Comment