The CreatorCon Call for Content is officially open! Get started here.

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

@Rocio 

Thank you for marking my response as helpful.

I believe I have answered your original question on getting last day of month

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

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

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Rocio,

Following will calculate date that's 3 days from the end of this month.

var gdt = new GlideDateTime(gs.endOfThisMonth());
var gt = new GlideTime();
gt.setValue("72:00:00");
gdt.subtract(gt);
gs.info(gdt.getDisplayValue());

Execution result. The time is off by 1 second.

*** Script: 2022-03-28 23:59:59