Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

how to calculate no: of mondays / tuesdays / wednesdays ... etc in a month

soumyadaggula
Tera Contributor

soumyadaggula_0-1669455465800.png

Hello team , 

I have a month field with drop down of months with values 1 - 12 , when I select a month ( ex :Jan ) and select 1st Monday , it should give me a count of how many Mondays are there in the selected month , and so on for Tuesday / wednesday .... 

I tried this script , but it does not work .

var dt = "audit_month";
var gdt = new GlideDateTime(dt);
var day = gdt.getDayOfWeek();
var days = gdt.getDayOfMonthLocalTime();

if ((day == 1 || day==2 || day==3) && ((days >=1 && days <=7))) {
g_form.setValue("u_days", days);

 

6 REPLIES 6

Saurabh Gupta
Kilo Patron

Hi @soumyadaggula 

 

You can try something like below script. Try to run the code in background script

getNumberofSpefWeekdays(11,2022,3)//Month,Year,dayofWeek
getNumberofSpefWeekdays(2,2024,4)//Month,Year,dayofWeek
function getNumberofSpefWeekdays(month,year,firstDay)
{
var dayObj={"1":"Monday","2":"Tuesday","3":"Wednesday","4":"Thursday","5":"Friday","6":"Saturday","7":"Suunday"}
var monthObj={"1":"January","2":"February","3":"March","4":"April","5":"May","6":"June","7":"July","8":"August","9":"September","10":"October","11":"November","12":"December"}

var count=0;
var dtStr=year+"-"+month+"-01 00:00:00"
var daysThisMonth = new GlideDateTime(dtStr);
for (var i = 1; i <= parseInt(getDaysInMonth(year,month)); i++)
{
    
if (daysThisMonth.getDayOfWeekUTC() == firstDay)
{
        count++;
}
daysThisMonth.addDaysUTC(1);

}

gs.info("In the year "+year+" of "+monthObj[month]+" total "+dayObj[firstDay]+": "+count)
}


function getDaysInMonth(year, month) {
  return new Date(year, month, 0).getDate();
}

 

 

 

 

 

 


Thanks and Regards,

Saurabh Gupta

thank you Saurab , this is working in background script 

You can write the same function in your logic and use this.




Thanks and Regards,

Saurabh Gupta

hello Saurab , 

 

My requirement is similar to above question , and I used the code as below , but it is not giving the out put , cloud you please help me :

 

vamshi233_0-1672665934591.pngvamshi233_1-1672665992952.png