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.






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: