I have one date field called "Billing date" and one Number Field called "Billing Delay". I want to update one Date field "DueDate" which computes "Billing date"+"Billing Delay"

for Eg:

"Billing date"=15.11.2016 "Billing Delay"=10 "DueDate"=25.11.2016

While Computation It will Exclude the Saturday and Sunday.

How to achieve this ?


for(BEV_BillingEvent__c BillingEvent:lstBillingEvent) {
    Date d=BillingEvent.Billingdate__c;
    Integer i=BillingEvent.BillingDelay__c;  

Try the below code. Its much simpler and covers your requirement (I believe).

    for(BEV_BillingEvent__c BillingEvent:lstBillingEvent) 


    Datetime dt = DateTime.newInstance(BillingEvent.Billingdate__c, 3, 3, 3,0);
    //system.debug('dt** = '+ dt);

        for (Integer i = 1; i <= BillingEvent.BillingDelay__c;) {
          dt = dt.addDays(1);
          system.debug('dt = '+ dt+dt.format('EEE'));
          if(dt.format('EEE') != 'Sun' && dt.format('EEE') != 'Sat') i++;
  • Using Business hours Objcect.How to implement this ? – uma451 Nov 17 '16 at 10:16

addDays(additionalDays) Adds the specified number of additional days to a Date.

Date methods

you can simply use addDays method

for(BEV_BillingEvent__c BillingEvent:lstBillingEvent) 
      Date d=BillingEvent.Billingdate__c.addDays(BillingEvent.BillingDelay__c);


For excluding the Saturday and Sunday use below method, You need to pass the start and end date.

this will return exact days in number with excluding Saturday and Sunday.

once you get that number. Add those many days in billing date.

public static Integer daysBetweenExcludingWeekends(Datetime startDate, Datetime endDate) {
    Integer i = 0;

    while (startDate < endDate) {
        if (startDate.format('E') != 'Sat' && startDate.format('E') != 'Sun') {
        startDate = startDate.addDays(1);

    return i;

Check this answer for more options https://salesforce.stackexchange.com/a/5355/18731

  • thank you..how to exclude the saturday and Sunday>? – uma451 Nov 15 '16 at 12:54
  • @uma451 check my updates... – Ratan Paul Nov 15 '16 at 13:04
  • Date EndDate=TRIG_DATE.addDays(Integer.ValueOf(VehObj.UnitOrder__r.CarSet__r.Order__r.BillingDelay__c)); system.debug('<EndDate<<'+EndDate); I got the value for start date and enddate.How to exclude the saturday and sunday ? am getting error – uma451 Nov 17 '16 at 4:30
  • It's working and giving the value.In my example i have given start date and End Date.totaly 7 days are working days.3 days are non working days.In your code i is returning the working days..Please help me out from this @Ratan – uma451 Nov 17 '16 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.