How to get last Day of specific month ....

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 12:50 AM
Hi there,
I struggle like always with the gdt ...
Having a script, that gets fed with a variable 'month' equal to -2 for (2 months ago).
Now I need to the the LAST DAY of THE given month ...
I.e. .. month = -2 ... so we would talk about JUNE ... and I need to get the Date like "30.06.2019" ...
Anyone could help me here please? Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 01:05 AM
Hi
Please try this script in background script, it worked perfectly fine for me :-
var date = new GlideDateTime();
date.setDayOfMonthUTC(35);
gs.print(date.getDayOfMonthUTC());
Regards,
Omkar Mone.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 01:23 AM
thnx. But I do not get it.
I have a VARIABLE coming in .. i.e. -2, -3 ... this way I will the relevant past month ... . For this one I need the complete date field ... .
Can't see how you code will work with this.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 01:10 AM
try with below code.
var lastday = function(y,m){
return new Date(y, m, 0).getDate();
}
gs.print(lastday(2019,6));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 01:12 AM
Hi Zod,
You can use the method to return the last of the month which you are passing as
var date = new Date(year, month +1, 0); // This will give you the last date as 30 June 2019.
Hope this is helpful.
Please mark as correct or helpful if this solves the purpose.
Mohit Kaushik
ServiceNow MVP (2023-2025)