How can I calculate the last day of the month? getDaysInMonthUTC() doesn't work.

Community Alums
Not applicable

Hii! 
I need to do the calculation that returns true 3 days before the end of the month. This was my idea:

var today = new GlideDateTime();
today = today.getDate();

var daysInMonth = new GlideDateTime();
daysInMonth.getDaysInMonthUTC();

var daysDiff = GlideDateTime.subtract(today, daysInMonth);


if (daysDiff == 3){
 answer = true;
}

but it is not working, I tried with today's date and that the dateDiff == 14 inside the if and it returns me 1970-01-01 13:44:50, any ideas?

 

6 REPLIES 6

Maik Skoddow
Tera Patron
Tera Patron

HI

I have not the proper code for you, but I always use the following approach: get a GlideDateTime object with the first day of the next month and then subtract 1 day.

Kind regards
Maik

Community Alums
Not applicable

Hi Maik! That could work for me, what method do you use to calculate the first day of the next month?

Abhijit4
Mega Sage

Hi,

Please try below, I have tested in PDI and its working fine.

vvar today = new GlideDateTime();
var todayDateInteger = today.getDayOfMonthUTC();
var lastDateInteger=today.getDaysInMonthUTC();
var diff=lastDateInteger-todayDateInteger
if (diff== 3){

answer = true;

}

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

like this

var gdt = new GlideDateTime(); 

var num = gdt.getDaysInMonthUTC();

var gdt = new GlideDateTime();

gdt.setDayOfMonthLocalTime(num);

gs.info(gdt);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader