Set date 10 days in the future and format to dd/mm/yyyy (e.g. 21/08/2010)

Julian picture Julian · Aug 26, 2010 · Viewed 46.9k times · Source

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.

Answer

Fenton picture Fenton · Aug 26, 2010

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/