I have two dateTime fields, I need to get the amount of days and hours that have elapsed. This is the best i Can do but now im getting some errors.
Heres the error
System.TypeException: Invalid decimal: 0.-22
If the first date is 22/09/2016 12:30 and the second is 24/09/2016 14:00 I would want the results to be 2.1
public static Decimal getTimeDifference(Datetime now, Datetime endTime){
if(now != null && endTime != null){
Long dt1Long = endTime.getTime();
Long dt2Long = now.getTime();
Long milliseconds = dt2Long - dt1Long;
Long seconds = milliseconds / 1000;
Long minutes = seconds / 60;
Long hours = minutes / 60;
Long days1 = hours / 24;
Long dt1Long2 = endTime.addDays((Integer)days1).getTime();
Long dt2Long2 = now.getTime();
Long milliseconds2 = dt2Long2 - dt1Long2;
Long seconds2 = milliseconds2 / 1000;
Long minutes2 = seconds2 / 60;
Long hours2 = minutes2 / 60;
Long days2 = hours2 / 24;
return Decimal.valueOf(days1 + '.' + hours2);
} else {
return null;
}
}