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

bernyalvarado
Mega Sage

Hi Samuel, try using gs



gs.endOfThisMonth();



Thanks,


Berny


the function worked, but my log is giving me 2017-09-01 instead of 2017-08-31 (the end of this month)


nishailame
ServiceNow Employee
ServiceNow Employee

https://community.servicenow.com/thread/225958



 


Thanks.


PS: Hit like, Helpful, Correct and Endorse, if it answers your question.


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