Date and GlideDate confusion

Wayne Richmond
Tera Guru

I have a weird issue between dev and prod instances. I have created a monthly scheduled job to run every three months:

var gdt = new GlideDate();

var mth = gdt.getMonth();

if (mth == 1 || mth == 4 || mth == 7 || mth == 10) { // Feb, May, Aug, Nov

answer = true

}

else {

answer = false;

}

In dev, this does not run despite the month being February (1). However, in prod, it runs just fine.

In dev, I changed the months in the condition to this:

if (mth == 0 || mth == 2 || mth == 6 || mth == 9)

It worked!

I then ran the following script as a background to confirm what mth was:

var gdt = new GlideDate();

var mth = gdt.getMonth();

gs.info(mth);

The output was: 2 (March?)

I then ran this:

var gdt = new Date();

var mth = gdt.getMonth();

gs.info(mth);

The output was 1 (February?)

This is the case in both instances.

So, why does the script work in prod and not in dev?

Why when I change the month in the condition does it work in dev?

What is the difference between Date and GlideDate?

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Hi Wayne,



That is how it's defined, If you use the GlideRecord (OOB Service now GlideRecord API) for getMonth() then it provides the numeric value of Months starting from 1 to 12: GlideDateTime - Global


GlideDateTime - getMonth()


Gets the month stored by the GlideDateTime object, expressed in Java Virtual Machine time zone.


Returns


Type                   Description


Number         The numerical value of the month, Jan=1, Dec=12.



But in general if you use the Javascript getMonth() function then it provides the numeric value of Months starting from 0 to 11.: JavaScript getMonth() Method


The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.



Please check the provided links for more details.


View solution in original post

10 REPLIES 10

Thanks. So why is the month different?


Here is the tricky point




date format different in dev and prod   this is the reason why you getting the month different




EX:



in dev date is 12/2/2018(DD/MM/yyyy)


in test date   1/12/2018(MM/DD/YYYY)


Thanks but the date formats are the same in both instances


Shishir Srivast
Mega Sage

Hi Wayne,



That is how it's defined, If you use the GlideRecord (OOB Service now GlideRecord API) for getMonth() then it provides the numeric value of Months starting from 1 to 12: GlideDateTime - Global


GlideDateTime - getMonth()


Gets the month stored by the GlideDateTime object, expressed in Java Virtual Machine time zone.


Returns


Type                   Description


Number         The numerical value of the month, Jan=1, Dec=12.



But in general if you use the Javascript getMonth() function then it provides the numeric value of Months starting from 0 to 11.: JavaScript getMonth() Method


The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.



Please check the provided links for more details.


Thanks, this is a very detailed explanation but I still don't understand by the following scheduled job ran successfully in prod today given I used GlideDateTime (which shouldn't have recognise 1 as the current month):



// Run every 3 months (when month is Jan (1), Apr (4), Jul (7) or Oct (10))


var gdt = new GlideDateTime();


var mth = gdt.getMonth();


if (mth == 1 || mth == 4 || mth == 7 || mth == 10) {


answer = true


}


else {


answer = false;


}



The time the job ran is 00:08:00. Could that make a difference (if server time was an hour behind maybe, therefore the previous day (and month))? I'm in the UK and our servers are located Amsterdam and Dublin.