Skip to main content
Hello,

I am trying to find a way to add decimal value to a date field in Apex. In my example, I want to add 1.5 days to a date field. AddDays or datefield+1.5 doesn't work as the add days is expected to be numeric. Do anyone have any alternatives to add decimal days to a date field?

Thanks

Shree
2 Antworten
  1. 13. Aug. 2020, 02:09
    other alternate would be to build a custom apex class which accepts the dateTime and a decimal as input and then does a break down of the decimal into two parts, first and integer and use the datetime.AddDays ();

     

    static DateTime addDecimalDays(DateTime dateTime, Decimal decimalDays){}

    so 

    Datetime newDateTime = DateTime.AddDays(Integer.valueOf(decimalDays));

    and then with the decimal component break it out to a number of hours and use dateTime.addhours();

    Decimal decimalDayComponent = decimalDays - Integer.valueOf(decimalDays);

    Integer roundedHours = Integer.valueOf(decimalDayComponent *24);

    newDateTime = DateTime.AddHours(roundedHours);

    return newDateTime;

    Regards

    Andrew

    p.s. code provide uncompiled and as-is

     
0/9000