VMware Cloud Community
NCGeeks
Contributor
Contributor

Weird Java Script issue

I am currently using vRO 7.2 and am running into this issue, ive browed around some javascript websites for anyone who has written something similar but I never seen any have an issue with there calling of dates past 13 .Is this something funky in vRO or am I missing something?

var date = new Date();
var future_date = new Date();
var date_start = 1;
var date_range = 14;
var date_range_array = new Array();

while ( date_start <= date_range){
future_date.setDate(date.getDate()+ date_start);
System.log(future_date);

var numberOfMonth = future_date.getMonth() + 1 ;
var day = future_date.getDate();
var year = future_date.getFullYear();
var month;

//Conversion to Month from number
//---------------------------------------------------
if (numberOfMonth == "1"){
month = "Jan";
}

else if (numberOfMonth == "2"){
month = "Feb";
}

else if (numberOfMonth == "3"){
month = "Mar";
}

else if (numberOfMonth == "4"){
month = "Apr";
}

else if (numberOfMonth == "5"){
month = "May";
}

else if (numberOfMonth == "6"){
month = "Jun";
}

else if (numberOfMonth == "7"){
month = "Jul";
}

else if (numberOfMonth == "8"){
month = "Aug";
}

else if (numberOfMonth == "9"){
month = "Sept";
}

else if (numberOfMonth == "10"){
month = "Oct";
}

else if (numberOfMonth == "11"){
month = "Nov";
}

else if (numberOfMonth == "12"){
month = "Dec";
}

//---------------------------------------------------

var combineDate = month + "-" + day + "-" + year;
System.log(combineDate);

//---------------------------------------------------
date_start++;

}

Goal is to have it generate the date in a certain format but when it gets past day 13 it gets a little weird.

[2017-07-19 15:03:51.146] [I] Thu Jul 20 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.147] [I] Jul-20-2017

[2017-07-19 15:03:51.148] [I] Fri Jul 21 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.150] [I] Jul-21-2017

[2017-07-19 15:03:51.151] [I] Sat Jul 22 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.152] [I] Jul-22-2017

[2017-07-19 15:03:51.154] [I] Sun Jul 23 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.155] [I] Jul-23-2017

[2017-07-19 15:03:51.157] [I] Mon Jul 24 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.158] [I] Jul-24-2017

[2017-07-19 15:03:51.160] [I] Tue Jul 25 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.161] [I] Jul-25-2017

[2017-07-19 15:03:51.163] [I] Wed Jul 26 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.164] [I] Jul-26-2017

[2017-07-19 15:03:51.165] [I] Thu Jul 27 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.167] [I] Jul-27-2017

[2017-07-19 15:03:51.168] [I] Fri Jul 28 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.169] [I] Jul-28-2017

[2017-07-19 15:03:51.171] [I] Sat Jul 29 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.172] [I] Jul-29-2017

[2017-07-19 15:03:51.174] [I] Sun Jul 30 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.175] [I] Jul-30-2017

[2017-07-19 15:03:51.176] [I] Mon Jul 31 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.178] [I] Jul-31-2017

[2017-07-19 15:03:51.179] [I] Tue Aug 01 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.180] [I] Aug-1-2017

[2017-07-19 15:03:51.181] [I] Sat Sep 02 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.183] [I] Sept-2-2017

[2017-07-19 15:03:51.184] [I] Wed Oct 04 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.185] [I] Oct-4-2017

[2017-07-19 15:03:51.186] [I] Sat Nov 04 2017 19:03:51 GMT-0000 (UTC)

[2017-07-19 15:03:51.190] [I] Nov-4-2017

Thanks for the help in advance!

0 Kudos
3 Replies
tschoergez
Leadership
Leadership

Check out the System.formatDate() method, it exposes the java date formatter to javascript.

The workflow Library/Troubleshooting/Export logs..... has some example:

zipFileName = zipFileName + "_" + System.formatDate(new Date(), "yyMMdd_HHmmss") + ".zip";

for full documentation check the java reference SimpleDateFormat (Java Platform SE 7 )  for the meanings of the characters.

For "counting" through the dates, check this article: Add days to JavaScript Date - Stack Overflow

Regards,

Joerg

0 Kudos
vROGeek
Contributor
Contributor

Thanks for the help. Ill dig in more closely to this article and see if i can change some things. I guess my confusion isnt on the format but on this.

Lets remove the format portion and look at the date itself:

var date = new Date();

var future_date = new Date();

var date_start = 1;

future_date.setDate(date.getDate()+ date_start); <-- if we make date_start 13 it lines up correctly with [2017-07-19 15:03:51.181] [I] Sat Sep 02 2017 19:03:51 GMT-0000 (UTC) however when we change that number to 14 instead of it being (date + 14) it switches to months [2017-07-19 15:03:51.184] [I] Wed Oct 04 2017 19:03:51 GMT-0000 (UTC).

0 Kudos
vROGeek
Contributor
Contributor

I redid it based on your suggestion and the site:

function addDays(date, days) {

  var result = new Date(date);

  result.setDate(result.getDate() + days);

  return result;

}

var future_date = new Date();

var date_start = 1;

var date_range = 30;

var date_range_array = new Array();

while (date_start <= date_range){

var new_date = addDays(future_date,date_start);

System.log(date_start);

System.log(new_date);

date_start++;

}

This seems to fix my issue and give me what i need.

Odd but ill take it Smiley Happy

Thanks again for the help!

0 Kudos