new GlideDateTime().getMonth not working properly

gayatri6
Tera Contributor

I need to get the current months of the 2 dates.  

I am trying to use the getMonthLocalTime(), getMonthUTC() and getMonth() so as to get the current month of the date entered. If the date is larger than 12, then it is working fine and returning the month number. HOWEVER, if the date is less than 13, i.e., 1-12, then it is getting confused and returning the date of month number instead of the month number. What should I do?

 

 

10 REPLIES 10

palanikumar
Mega Sage

Hi,

Looks like the issue is due to date format. Try the Below code:

var strDate = "13/10/2020";
var dt = new GlideDateTime();
dt.setDisplayValue(strDate,"dd/MM/yyyy");
gs.print(dt.getMonthLocalTime());

ServiceNow will consider first part of date as month. If it is greater than 12 then it consider it as day and take second part as month. Set the date format based on your input so that you can force the dateformat.

Thank you,

Palani

Thank you,
Palani