How can we convert todays date to a workday

JPSS
Tera Contributor
I need to convert todays date to a workday. example 5th februvary is the 3rd workday of that month
1 ACCEPTED SOLUTION

Hi @JPSS   yes the function as written should convert today's date to workday (of current month)

Give it a try in Background scripts

 

--

Bala

View solution in original post

6 REPLIES 6

Hi @JPSS   yes the function as written should convert today's date to workday (of current month)

Give it a try in Background scripts

 

--

Bala

Hello here is the code additional comments.

(function numberOfWorkdays() {
var gdt = new GlideDateTime();   // get current date time object
var oneday = 24 * 60 * 60 * 1000;  // number of milliseconds in a day

var cmon = gdt.getMonthLocalTime();   // get today's month
var mon = cmon;     // initialize variable to keep track of month for previous days  
var wdays = 0;        // initialize counter to keep track of number of workdays

while ( mon === cmon ) {    // loop until the current month is different from today's month 

    var dofw = gdt.getDayOfWeekLocalTime();      // day of week of current date
    if( dofw >= 1 && dofw <= 5) wdays++;           // increment counter if it is a weekday  ( Mon = 1  Sunday =7)
    gdt.subtract(oneday);                               // go to previous day by subtracting 1 day from current date
    mon = gdt.getMonthLocalTime();        // get month of current (modified) date
}
return wdays;
}())

 

Hope the comments help

--

Bala