calculate hours difference

Category: Crystal Reports 9 through 2016
Forum Name: Technical Questions
Forum Discription: Formulas, charting data, Crystal syntax, etc.
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=22876
Printed Date: 26 Sep 2020 at 11:22pm

Topic: calculate hours difference
Posted By: sanchezgmc06
Subject: calculate hours difference
Date Posted: 16 Jun 2020 at 9:41am
 hi I need to be able to calculate the difference in hours form 2 separate date and time fields fields Date 1 Time 1 Date 2 Time 2 I attempted using the following formula however I dont believe the date and time are stored in the date field. local numbervar tt := datediff('h',date1,date2); local numbervar d := truncate(tt/24,0); local numbervar h := remainder(tt,24); local stringvar tt_d := totext(d,0,"") + ' days '; local stringvar tt_h := totext(h,0,"") + ' hours'; local stringvar tt_string := tt_d & tt_h thank you!!

Replies:
Posted By: kevlray
Date Posted: 18 Jun 2020 at 4:58am
 If there is no time component tied to the fields in question.  Then thee is no way to properly do a hour difference.Have you done test formulas to see what intermediate values you get?Also there no reason to calculate the days form the hours.  You could do a date diff of days an put that in variable.

Posted By: sanchezgmc06
Date Posted: 18 Jun 2020 at 5:49am
 This ended up working for me Thank you for your help DateDiff("h", DateTime({StartDate},{StartTime}), DateTime({EndDate},{EndTime})) & " Hours and " & DateDiff("n", DateTime({StartDate},{StartTime}), DateTime({EndDate},{EndTime})) Mod 60 & " Minutes"