Set date 10 days in the future and format to dd/mm

2020-01-29 06:09发布

问题:

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.

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.

回答1:

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/



回答2:

Try:

new Date(Date.now() + 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 10)


回答3:

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:

new Date((new Date()).getTime() + (10 * 86400000))


回答4:

What I would do, is create a custom DateHelper object that looks like this :

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)



回答5:

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 date range based on the current date.

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

Then the next day, on August 29, 2017, the date range will read August 22 thru September 21, 2017

Note: if necessary you can modify the code to make either the start or future date static.

To use it in HTML code:

The event is held: <script>document.write(dateRange)</script>

will yield:

The event is held: August 21 thru September 20, 2017

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


回答6:

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();