Finding the last day of the month

David Yearsley
Tera Expert

Building a billing application with a scheduled job that will build a CSV file. The issue is the call gs.endOfThisMonth(); returns the value in GMT and for our instance it is 6 or 7 am on the 1st depending on Daylight Savings time. Is there an easy way to use this function to return the local end of the month or should a different way be used.

gs.endOfThisMonth(); returns 2020-02-01 06:59:59 a any day in January.

Thanks

 

1 ACCEPTED SOLUTION
4 REPLIES 4

Sumanth16
Kilo Patron

Using setDayOfMonthUTC() was the easiest way. I just did gd = gd.setDayOfMonthUTC(31); and it always returns the last day of the month. 

I was never able to get gs.endOfThisMonth(); to actually return the last day of the month. The only way this would work for this project is if you did:

gd.endOfThisMonth();

gd.addDaysLocalTime(-1); This would work because the current project does not care about the time.

There needs to be a new call gd.endOfThisMonthLocalTime();

 

kente
Tera Guru

 

I am a little puzzled about the use of gs.endOfThisMonth(); when your speaking of scheduled aswell?

But you could a "combo" of the below.

So first find the number of days in month 

var gdt = new GlideDateTime(); 
gs.info(gdt.getDaysInMonthUTC());

Then find the last day 

var gdt = new GlideDateTime();
gdt.setDayOfMonthLocalTime(whatevernumberofdate);

Ian Mildon
Tera Guru

You could use this to calculate (and set) the last working day of the month, in "local time"

function isLastWorkingDay(gdt) {

    var today = gdt.getDayOfWeekLocalTime(); 
    var thisMonth = gdt.getMonthLocalTime();
    
    switch (today) {
    // Saturday and Sunday are NOT the last working day of the month
    case 6:
    case 7:
    return false;

    break;
     
    // If it's a Friday, add 3 and see if the month changes.
    case 5:
    var nextMonday = gdt;
    nextMonday.addDaysLocalTime(3);
    var nextMondayMonth = nextMonday.getMonthLocalTime();
    return (nextMondayMonth != thisMonth);
 
    break;
    
    // If it's a Mon-Thu add one and see if the month changes
    default: 
    var nextDay = gdt;
    nextDay.addDaysLocalTime(1);
    var nextDayMonth = nextDay.getMonthLocalTime();
    return (nextDayMonth != thisMonth);
    
    break;
    }
}
     
    var now = new GlideDateTime();
    var lwd = isLastWorkingDay(now);
    gs.info('Last working day=' + lwd);