简体   繁体   中英

How to calculate a date from user input date

How to calculate a date from date inputs using javascript? Here is my code:

<p>
  Start date: <input id="start" name="start" placeholder="Start Date(mm/dd/yyyy)" type="date" />
</p>
<p>
  End date: <input id="end" name="end" placeholder="End Date(mm/dd/yyyy)" type="date" />
</p>

I want to display a date by adding 17 months to the end date

Use following and add 17 months

var myDate = new Date();
myDate.setMonth( myDate.getMonth() + 17 );
alert(myDate);

With the MomentJS library you can do it with:

JSFiddle

var yourDate = new Date(),
    newDate = moment(yourDate).add(17, 'months');

console.log(newDate.format());

Get the value of the end date with JQuery like this:

var endDate = $("#end").val();

Then turn it into a date object:

endDate = new Date(endDate);

You can then add 515 (365 for 12 months, and 300 for the extra 5 months approx.) days to the date like this:

endDate.setDate(endDate.getDate()+515);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM