By Julian


2010-08-26 06:34:10 8 Comments

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.

6 comments

@HaNdTriX 2016-04-14 21:01:58

Try:

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

@Ulugov 2018-07-06 10:23:25

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

@Sammy 2017-08-29 14:37:32

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

@John Slegers 2016-03-09 14:50:25

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)

@Jayd 2015-10-14 14:57:37

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

@MasterP 2016-07-27 15:39:26

This brings back the incorrect date if you use certain dates.

@Fenton 2010-08-26 06:48:24

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/

@John Magnolia 2014-09-18 18:05:12

Why is it not possible to use format or toString after using setDate?

Related Questions

Sponsored Content

57 Answered Questions

[SOLVED] How to format a JavaScript date

46 Answered Questions

[SOLVED] Add days to JavaScript Date

25 Answered Questions

[SOLVED] Javascript add leading zeroes to date

13 Answered Questions

[SOLVED] YYYY-MM-DD format date in shell script

15 Answered Questions

[SOLVED] Calculate difference between two dates (number of days)?

  • 2009-10-22 13:47:15
  • leora
  • 1174174 View
  • 1113 Score
  • 15 Answer
  • Tags:   c# date

6 Answered Questions

[SOLVED] How to subtract a day from a date?

17 Answered Questions

[SOLVED] How to decide when to use Node.js?

3 Answered Questions

Sponsored Content