You need to sign in to do that
Don't have an account?
Prakhar 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?
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?
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
please find below code
.
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
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);
}
}
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'))