简体   繁体   English

从输入的天数中加上今天的日期

[英]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 我正在寻找一些JavaScript,用户在其中将数字输入表单中的数字然后从今天的日期计算日期

For example 例如

User enters 10 Then it shows the date as 17/03/2013 用户输入10然后将日期显示为17/03/2013

10 days ahead of today's date 比今天早10天

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; 您应将天数添加到javascript类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! 例如,如果今天是8号,您加10,您将获得18号,但是如果今天是24号,并且您加10,您将获得34号!

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 我为浏览器和/或nodejs编写了一个使用了一段时间的https://github.com/deitch/jsorm-i18n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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