简体   繁体   中英

Add today's date from numbers of days entered in input

I am looking for some javascript where the user enters a number into a input box in a form then it calucates the date from today's date

For example

User enters 10 Then it shows the date as 17/03/2013

10 days ahead of today's date

var result = new Date();
result.setDate(result.getDate() + 10);

Try this,

var add_days = parseInt(document.getElementById('text1').value);
var now = new Date();
now.setDate(now.getDate()+add_days);
alert(now); // Display this now to wherever you want

You should add de amount of days to javascript class Date;

var addDays = $('#addDaysText').text();
var currentDate = new Date(); //get current date
var futureDate = currentDate() + addDays;

Most of those answers will work, unless you start crossing month lines. For example, if today is the 8th, and you add 10, you will get the 18th, but if today is the 24th and you add 10, you will get the 34th!

The right way to do this is to convert it to an absolute time format, add the unit, then convert back.

var now = new Date().getTime();
var diff = 10; // days
var futureTime = now + 10*24*60*60*1000; // 24 hrs/day, 60 mins/hr, 60 sec/min, 1000 ms/sec
var future = new Date(futureTime);

If you want to start doing some really strange stuff, I would recommend using a library. I authored one for browser and/or nodejs use a while back https://github.com/deitch/jsorm-i18n

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