+ Start a Discussion
Prakhar KPrakhar K 

I am trying to exclude the 'Saturday' and 'Sunday' but the apex is unable to detect the weekends?

In Apex I am trying to count the number of days from first day of the current month excluding Sat and Sun.

            Date firstDayOfMonth = System.today().toStartOfMonth();
             Datetime dt1 = DateTime.newInstance(firstDayOfMonth, Time.newInstance(0, 0, 0, 0));
             String d=dt1.format('EEEE');
            
             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');
                e=(String)et1.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);  
                } 
  
When the days are Sat or Sun ,it should not go inside if conditions but its going inside ifs.Where I am wrong please guide me?
 
Best Answer chosen by Prakhar K
suresh sanneboina 4suresh sanneboina 4
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);  
    } 
}

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi Kumar,

please find below code
.
public class CalculateDayBetweenTwoDates {
    public static Integer calculateDays(Date dtStartDate, Date stEndDate, set<Date>){

        Integer intCount = 0;
        while(dtStartDate < stEndDate){
            dtStartDate = dtStartDate.addDays(1);            
            datetime myDate = datetime.newInstance(dtStartDate.year(), 
                                                   dtStartDate.month(),
                                                     dtStartDate.day());
            if(myDate.format('EEEE') != 'Sunday' && myDate.format('EEEE') != 'Saturday' ){
                intCount++;
            }
        }
        return intCount;
    }
}

​Hope this helps you.

Please accept my solution as Best Answer if my answer was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 


 
 
 
suresh sanneboina 4suresh sanneboina 4
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);  
    } 
}
This was selected as the best answer
Prakhar KPrakhar K
Hi Suresh,
Thanks for your response in such a short time!!!
It worked!!
for those who did not come to know what the change was:
 replaced           if (d!='Saturday' || d!='Sunday')    by  if (!(d =='Saturday' || d == 'Sunday'))