how to calculate no: of mondays / tuesdays / wednesdays ... etc in a month
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2022 01:43 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2022 02:46 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2022 03:11 AM
thank you Saurab , this is working in background script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2022 03:13 AM
You can write the same function in your logic and use this.
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2023 05:27 AM
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 :