How to get last day of the last month

vspk
Giga Expert

Hi All,

 

As part of integration, we will get some data monthly once into ServiceNow table.

'Usage date' is one of the field on the table [ date type field].

Our client wants to have this populated as last day of the last month.

For eg., if data arrives on June 25th, 2020, 

Usage date should store it as May 31st, 2020.

 

could anyone help me with this requirement.

 

Regards,

Venkata

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

Try this. 

var month = gs.endOfLastMonth();
var monthd = new GlideDateTime(month);
monthd.addDays(-1);
gs.info(monthd);

 

output

find_real_file.png

Please mark this correct & helpful if it answered your question.

Thanks & Regards,
Sharjeel

 

Regards,
Muhammad

View solution in original post

5 REPLIES 5

Muralidharan BS
Mega Sage
Mega Sage

Hi Venkata,

You can use this 

var month = gs.endOfLastMonth();
gs.info(month);

 

result

find_real_file.png

Its coming as June 1st to me:

find_real_file.png

ok, may be this will help

var month = gs.endOfLastMonth();
var gdt = new GlideDateTime(month);
gdt.addDaysLocalTime(-1);
gs.info(gdt.getLocalDate());

Gaurav Shirsat
Mega Sage

Hi VSPK

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.

for example.:-

var gdt = new GlideDateTime();

gdt.setDayOfMonthUTC(9);

gs.print(gdt.getDayOfMonthUTC());

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_GlideDateTimeAPI#r_G...

https://community.servicenow.com/community?id=community_question&sys_id=a19b8b61db9cdbc01dcaf3231f96...

Please Mark Correct and Helpful

Thanks and Regards

Gaurav Shirsat