38

我真的很感激创建一些最终将在 Selenium 中使用的 JavaScript 的帮助,它会自动设置比当前日期提前 10 天的日期,并以以下格式显示 dd/mm/yyyy。

我目前有下面的脚本,但我没有得到任何地方:

var myDate=new Date();
myDate.now.format(myDate.setDate(myDate.getDate()+5),("dd/mm/yyyy");

任何帮助将非常感激。

4

6 回答 6

41

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.svendtofte.com/javascript/javascript-date-string-formatting/

于 2010-08-26T06:48:24.643 回答
30

尝试:

new Date(Date.now() + 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 10)
于 2016-04-14T21:01:58.740 回答
9

我需要做这样的事情,但我需要在线结果。所以这对我来说是从现在起 10 天后得到日期的方法:

new Date((new Date()).getTime() + (10 * 86400000))
于 2015-10-14T14:57:37.860 回答
3

我要做的是创建一个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));

(另见这个小提琴

于 2016-03-09T14:50:25.103 回答
-1

这个原始页面主题来自 2010 年。因此,我在 2017 年 8 月添加了此回复。这是一个简单的解决方案,适用于根据当前日期创建滚动日期范围。

例如:如果今天是 2017 年 8 月 28 日,并且您想要过去 -7 天的开始日期和未来 23 天的未来日期,那么以下 JavaScript 代码将输出日期范围字符串:8 月 21 日至2017 年 9 月 20 日

然后第二天,即 2017 年 8 月 29 日,日期范围为 2017 年8 月 22 日至 9 月 21 日

注意:如有必要,您可以修改代码以使开始日期或未来日期为静态。

要在 HTML 代码中使用它:

活动举办:<script>document.write(dateRange)</script>

将产生:

活动举办时间: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
于 2017-08-29T14:37:32.767 回答
-1

我需要通过添加月份来获得未来的年份:

var duration = 13;
var month = 11;
var year = 2018;
var final_year = new Date(year, month + duration);
document.body.innerHTML = final_year.getFullYear();

于 2018-07-06T10:23:25.150 回答