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

6

38

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");
Q answered 26/8, 2010 at 6:34 Comment(2)
what is the error or problem you are facing?Gliwice
I have to point out a bit of geek-irony here. A JavsScript date question posted by "Julian" - all dates in JavaScript are based on the Julian Date system en.wikipedia.org/wiki/Julian_day#Julian_DateExurbanite
E
44

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/

Exurbanite answered 26/8, 2010 at 6:48 Comment(3)
Why is it not possible to use format or toString after using setDate?Shawnshawna
Does the above method skip dates like 30,31 febDutra
@JohnMagnolia Use below Snippet for String Output var targetDate = new Date(); targetDate.setDate(targetDate.getDate() + 30); var dd = String(targetDate.getDate()).padStart(2, '0'); var mm = String(targetDate.getMonth() + 1).padStart(2, '0'); var yyyy = targetDate.getFullYear(); let dateStr: string = mm + '/' + dd + '/' + yyyy; console.log(dateStr)Deviltry
P
38

Try:

new Date(Date.now() + 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 10)
Pandemic answered 14/4, 2016 at 21:1 Comment(0)
Z
11

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))
Zebec answered 14/10, 2015 at 14:57 Comment(2)
This brings back the incorrect date if you use certain dates.Amarette
new Date(Date.now() + (10 * 86400000)) also worksUpstage
U
3

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)

Uniocular answered 9/3, 2016 at 14:50 Comment(0)
B
-1

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
Bridge answered 29/8, 2017 at 14:37 Comment(0)
K
-1

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();
Knowitall answered 6/7, 2018 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.