0
我有兩個dateTime字段,我需要獲取已過去的天數和小時數。這是我能做的最好的,但現在我得到一些錯誤。以十進制格式獲取兩個日期時間之間的日期和小時
下面有錯誤
System.TypeException:無效十進制:0.-22
如果第一日期爲22/09/2016 12:30,第二個是24/09/2016 14:00我要的結果是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;
}
}
這已經在這裏找到答案.. http://salesforce.stackexchange.com/questions/5354 /天之間,二,日期時間值 - 不含-週末 – EricSSH