[Demo] Add Days to a Date in JavaScript

[Demo] Add Days to a Date in JavaScript

by Sahil Kashyap

HTML

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<h2>Select a Date in first TextBox. Then click the Button to see how 3 days is added to this Date and shown in "Follow Date" TextBox.</h2>

<p>Date:
    <input id="start_date" type="text" onchange="getdate()"/>
</p>

<p>Follow Date:
    <input id="end_date" type="text" />
</p>

JavaScript

$(document).ready(function () {
    $('#start_date').datepicker();
    $('#end_date').datepicker();
});

function getdate() {
    var tt = document.getElementById('start_date').value;

    var date = new Date(tt);
    var newdate = new Date(date);

    newdate.setDate(newdate.getDate() + 3);
    
    var dd = newdate.getDate();
    var mm = newdate.getMonth() + 1;
    var y = newdate.getFullYear();

    var someFormattedDate = mm + '/' + dd + '/' + y;
    document.getElementById('end_date').value = tt;
}