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

Zod
Giga Guru

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!

 

4 REPLIES 4

Omkar Mone
Mega Sage

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.

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. 

Harsh Vardhan
Giga Patron

try with below code. 

 

var lastday = function(y,m){
return  new Date(y, m, 0).getDate();
}
gs.print(lastday(2019,6));

Mohit Kaushik
Mega Sage
Mega Sage

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.

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)