How do I get last day of current month?

samuelscott
Tera Expert

Hi guys!

 

I'm currently trying to get the last day of the month (current month), however, I can't seem to make the function "endOfThisMonth()" to work and I couldn't fin any examples on the internet. Does anyone know how to make this work?

 

I'll attach my code which runs from a business rule.

 

(function executeRule(current, previous /*null when async*/) {     var queryDate = new GlideDateTime();         var dayOfMonth = 0;         var expenseLine = new GlideRecord('fm_expense_line');   dayOfMonth = queryDate.getDayOfMonthUTC();             if (expenseLine.get('sys_id', current.u_expense_line)) {                     expenseLine.u_date_query_closed = queryDate.getDisplayValueInternal();     gs.log("If # 1 is true: ", "1C");                       if (dayOfMonth <= 10) {     queryDate.endOfThisMonth();     expenseLine.u_new_payment_due_date = queryDate;     gs.log("If # 2 is true: "+ queryDate, "1C");             }           else{     queryDate.addDaysLocalTime(30);     expenseLine.u_new_payment_due_date = queryDate;     }         expenseLine.update();       }             gs.log("Day of the Month: " + dayOfMonth, "1C");         gs.log("Date query closed: " + expenseLine.u_date_query_closed, "1C");         gs.log("New Payment Due Date: " + queryDate, "1C");       })(current, previous);       
1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Samuel,



Check out setDayOfMonthUTC(). If you set it to something like 35, you'll get the last day of the month. Also, take a look at getDayOfMonthUTC().



The day of month to change to, from 1 to 31. If this value is greater than the maximum number of days in the month, the value is set to the last day of the month.


https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_GDT-setDayOfMonthUTC_N


View solution in original post

7 REPLIES 7

this worked perfectly! thanks!


bernyalvarado
Mega Sage

The output will be in GMT with the format: yyyy-mm-dd huh:mm:ss


*** Script: 2017-09-01 06:59:59

Nagaraja KB
Kilo Contributor

Please try this :

 

var gdt = new GlideDateTime();

gs.print(gdt.getDayOfMonth());