You need to sign in to do that
Don't have an account?
not able to calculate weekends from start date to end date in apex.
Hello Developers!
I am not able to calculate saturday if date is from friday to monday means 6/1/2012 to 9/1/2012 its total shows 3.
but if date is from friday to tuesday means 6/1/2012 to 10/1/2012 its total shows 3 its exclude both saturaday & sunday (working fine).
so i am not able to understand why is it calculating like that:
here are code for that:
DateTime dateInTheRange;
for(integer j = 0; j< calculatedNoOfDays; j++)
{
dateInTheRange = startDate.addDays(j);
if(dateInTheRange.format('E')=='Sat'| dateInTheRange.format('E')=='Sun')
{
calculatedNoOfDays--;
}
}
Thanks in Advance!
Regards,
Ashish
Hi,
I am getting thecorrect value for a month’s using this code. Try the below sample code:
date startDate = date.newInstance(2008, 1, 1);
date dueDate = date.newInstance(2008, 1, 30);
integer numberDaysDue = startDate.daysBetween(dueDate);
integer count=0;
for(integer j = 0; j<= numberDaysDue; j++)
{
datetime dateInTheRange = startDate.addDays(j);
if(!(dateInTheRange.format('E')=='Sat'| dateInTheRange.format('E')=='Sun'))
{
count++;
}
}
system.debug('@@@@@@@@@@@@@@@'+count);
All Answers
Hi,
You can use below code sample as reference:
datetime startdate=system.now();
string st=startdate.format('E');
system.debug('@@@@@@@@@@@@@' +st);
integer count=0;
system.debug('$$$$$$$$$$$' +endate);
for(integer j = 0; j<7; j++)
{
date dateInTheRange = startDate.addDays(j);
if(!(dateInTheRange.format('E')=='Sat'| dateInTheRange.format('E')=='Sun'))
{
count++;
}
}
system.debug('@@@@@@@@@@@@@' +count);
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Hi,
Thanks for responding !
I have tried your coding reference its working fine for 1 week leave but if tried to take a leave from more than a week its also counting saturday and sunday!!
Means 6/1/2012 to 13/1/2012 = 6 (1 week working fine)
Means 6/1/2012 to 15/1/2012 = 8 ( more than a week) expecting value 6 bt showing 8.
just i changes in for loop j<7 and then its working fine for 1 week
here are the code which i changes:
DateTime dateInTheRange;
for(integer j = 0; j< 7; j++)
{
dateInTheRange = startDate.addDays(j);
if (dateInTheRange.format('E')=='Sat'| dateInTheRange.format('E')=='Sun')
{
calculatedNoOfDays--;
}
}
return calculatedNoOfDays;
}
}
regards,
ashish
Hi,
I am getting thecorrect value for a month’s using this code. Try the below sample code:
date startDate = date.newInstance(2008, 1, 1);
date dueDate = date.newInstance(2008, 1, 30);
integer numberDaysDue = startDate.daysBetween(dueDate);
integer count=0;
for(integer j = 0; j<= numberDaysDue; j++)
{
datetime dateInTheRange = startDate.addDays(j);
if(!(dateInTheRange.format('E')=='Sat'| dateInTheRange.format('E')=='Sun'))
{
count++;
}
}
system.debug('@@@@@@@@@@@@@@@'+count);