Javascript 将日期设置为未来 10 天,格式为 dd/mm/yyyy(例如 21/08/2010)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3572561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set date 10 days in the future and format to dd/mm/yyyy (e.g. 21/08/2010)
提问by Julian
I would really appreciate some help creating some JavaScript that will eventually be used in Selenium that automatically sets a date 10 days ahead from the current date and displays in the following format dd/mm/yyyy.
我非常感谢帮助创建一些最终将在 Selenium 中使用的 JavaScript,它会自动设置一个比当前日期提前 10 天的日期,并以以下格式 dd/mm/yyyy 显示。
I currently have the script below, but I'm not getting anywhere with it :
我目前有下面的脚本,但我没有得到任何地方:
var myDate=new Date();
myDate.now.format(myDate.setDate(myDate.getDate()+5),("dd/mm/yyyy");
Any help would be much appreciated.
任何帮助将非常感激。
回答by Fenton
Here is an example of getting the future date...
这是获取未来日期的示例...
var targetDate = new Date();
targetDate.setDate(targetDate.getDate() + 10);
// So you can see the date we have created
alert(targetDate);
var dd = targetDate.getDate();
var mm = targetDate.getMonth() + 1; // 0 is January, so we must add 1
var yyyy = targetDate.getFullYear();
var dateString = dd + "/" + mm + "/" + yyyy;
// So you can see the output
alert(dateString);
There are some more graceful ways to format dates, examples can be found at the following destinations:
有一些更优雅的日期格式,示例可以在以下目的地找到:
http://www.west-wind.com/Weblog/posts/282495.aspx
http://www.west-wind.com/Weblog/posts/282495.aspx
http://www.svendtofte.com/javascript/javascript-date-string-formatting/
http://www.svendtofte.com/javascript/javascript-date-string-formatting/
回答by HaNdTriX
Try:
尝试:
new Date(Date.now() + 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 10)
回答by John Slegers
What I would do, is create a custom DateHelperobject that looks like this :
我要做的是创建一个如下所示的自定义DateHelper对象:
var DateHelper = {
addDays : function(aDate, numberOfDays) {
aDate.setDate(aDate.getDate() + numberOfDays); // Add numberOfDays
return aDate; // Return the date
},
format : function format(date) {
return [
("0" + date.getDate()).slice(-2), // Get day and pad it with zeroes
("0" + (date.getMonth()+1)).slice(-2), // Get month and pad it with zeroes
date.getFullYear() // Get full year
].join('/'); // Glue the pieces together
}
}
// With this helper, you can now just use one line of readable code to :
// ---------------------------------------------------------------------
// 1. Get the current date
// 2. Add 10 days
// 3. Format it
// 4. Output it
// ---------------------------------------------------------------------
document.body.innerHTML = DateHelper.format(DateHelper.addDays(new Date(), 10));
(see also this Fiddle)
(另见这个小提琴)
回答by Jayd
I needed to do something like this, but I needed the result in-line. So this is what worked for me to get the date 10 days from now:
我需要做这样的事情,但我需要在线结果。所以这对我来说是从现在起 10 天后得到日期的方法:
new Date((new Date()).getTime() + (10 * 86400000))
回答by Sammy
This original page topic is from 2010. So, I added this reply in August 2017. It's a simple solution that works well for creating a rolling daterange based on the current date.
这个原始页面主题来自 2010 年。所以,我在 2017 年 8 月添加了这个回复。这是一个简单的解决方案,适用于基于当前日期创建滚动日期范围。
For example: If today is August 28, 2017 and you want a start date of -7 days in the past and a future date of 23 days in the future, then the following JavaScript code will output a date range string of: August 21 thru September 20, 2017
例如:如果今天是 2017 年 8 月 28 日,并且您想要过去 -7 天的开始日期和未来 23 天的未来日期,那么以下 JavaScript 代码将输出日期范围字符串:August 21 thru 2017 年 9 月 20 日
Then the next day, on August 29, 2017, the date range will read August 22 thru September 21, 2017
然后第二天,即 2017 年 8 月 29 日,日期范围将为 2017 年8 月 22 日至 2017 年 9 月 21 日
Note: if necessary you can modify the code to make either the start or future date static.
注意:如有必要,您可以修改代码以将开始日期或未来日期设为静态。
To use it in HTML code:
要在 HTML 代码中使用它:
The event is held: <script>document.write(dateRange)</script>
活动举行: <script>document.write(dateRange)</script>
will yield:
将产生:
The event is held: August 21 thru September 20, 2017
活动时间:2017年8月21日至9月20日
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var startDate = new Date();
startDate.setDate(startDate.getDate() -7 );
var futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 23);
var m1 = month[startDate.getMonth()];
var d1 = startDate.getDate();
var y1 = startDate.getFullYear();
var m2 = month[futureDate.getMonth()];
var d2 = futureDate.getDate();
var y2 = futureDate.getFullYear();
var dateRange = m1 + ' ' + d1 + ' thru ' + m2 + ' ' + d2 + ', ' + y2;
// alert(dateRange); use alert function to test the date range
回答by Ulugov
I needed to get future year by adding months:
我需要通过添加月份来获得未来的一年:
var duration = 13;
var month = 11;
var year = 2018;
var final_year = new Date(year, month + duration);
document.body.innerHTML = final_year.getFullYear();

