function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakesh M 20Rakesh M 20 

Hi All, I am trying to exclude weekends in number of days in the month. This is what i have written for that, can anyone suggest how can i do that please ?

Integer Month = Date.Today().Month();
        Integer Year = Date.Today().Year();
        Integer numberDays = date.daysInMonth(Year, Month);
        System.debug('Number of Days In Month--' +numberDays);
        
        Date d = System.today();
        Datetime dt = (DateTime)d;
        String dayOfWeek = dt.format('EEEE');
        Integer numberofdays;
        for(Integer i=0;i<numberofdays;i++)
        {
        if(dayOfWeek=='Saturday' || dayOfWeek=='Sunday')
           {
              numberofdays=numberDays--;
               System.debug('Number of Days After removing Weekends -- '+numberofdays);
           }
        }
ShirishaShirisha (Salesforce Developers) 
Hi Rakesh,

Greetings!

Please refer this thread and sample code provided below:
Date firstDayOfMonth = System.today().toStartOfMonth();
Datetime dt1 = DateTime.newInstance(firstDayOfMonth, Time.newInstance(0, 0, 0, 0));
String d=dt1.format('EEEE');
System.debug(d);
if(d != 'Saturday' &&  d != 'Sunday')
{
    Datetime et1 = DateTime.newInstance(Date.Today(), Time.newInstance(0, 0, 0, 0));
    String e=et1.format('EEEE');
    System.debug('After conversion present day======'+et1);
     
    Integer noOfDaysFromFirstDyMonth=0; 
    while (dt1<= et1) {
    d=(String)dt1.format('EEEE');
        if (!(d =='Saturday' || d == 'Sunday'))  
        {  
            if (!(e =='Saturday' || e =='Sunday'))
            {
                noOfDaysFromFirstDyMonth = noOfDaysFromFirstDyMonth + 1;  
                System.debug('dt1=='+dt1.format('EEE'));
                System.debug('et1=='+et1.format('EEE'));
            }
        }  
        dt1= dt1.addDays(1);  
    } 
}

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Malika Pathak 9Malika Pathak 9

Hi Rakesh,
Your for lopp would not work because intially there is nothhing in numberofdays.
Please find the solution I hope It will help You.


 

public static void NumberOfDaysExcludeWeekends(){
        Date day=Date.today();
        Integer Month = Date.Today().Month();
        Integer Year = Date.Today().Year();
        Integer numberDays = date.daysInMonth(Year, Month);
        
        System.debug('Number of Days In Month--' +numberDays);
        
        
        List<String> daysList=new List<String>();
        for(Integer i=1;i<=numberDays;i++)
        {
            Date d = date.newInstance(Year, Month, i);
            Datetime dt = (DateTime)d;
            String dayOfWeek = dt.format('EEEE');
            daysList.add(dayOfWeek);
        }
        
        system.debug('daysList---'+daysList.size());
        integer excludeWeekend=0;
        for(Integer i=0;i<numberDays;i++){
            
            if(daysList.get(i)=='Saturday' || daysList.get(i)=='Sunday')
            {
                system.debug('WeekEnd==> '+daysList.get(i));
                excludeWeekend++;
            }
        }
        integer ans=numberDays-excludeWeekend;
        system.debug(ans);
    }
 

If It will help you please mark it best answer so that it can help others in the future.

Thanks

Rakesh M 20Rakesh M 20
Thanks Malika Pathak 9