424,948 Members | 852 Online
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

# How to calculate the Time Difference between two dates

 P: 4 Hello, I have been trying to calculate the difference between two date and display the difference in hours and minutes (HH:MM). I can't get it calculate properly and I can't hours and minutes to display. I tried "results=enddate-startdate" dates have month, day, year,and time of day. After this calculation I end up with no results. Any help would be appreciated. Walt May 20 '07 #1
7 Replies

 Expert 5K+ P: 8,619 Hello, I have been trying to calculate the difference between two date and display the difference in hours and minutes (HH:MM). I can't get it calculate properly and I can't hours and minutes to display. I tried "results=enddate-startdate" dates have month, day, year,and time of day. After this calculation I end up with no results. Any help would be appreciated. Walt I'll illustrate 1 Method by code: Expand|Select|Wrap|Line Numbers Dim dteStartDateTime As Date, dteEndDateTime As Date dteStartDateTime = #5/20/2007 7:00:00 AM# dteEndDateTime = #5/20/2007 11:15:00 AM#   Debug.Print "Difference in Hours: " & DateDiff("h", dteStartDateTime, dteEndDateTime) Debug.Print "Difference in Minutes: " & DateDiff("n", dteStartDateTime, dteEndDateTime) - _             (DateDiff("h", dteStartDateTime, dteEndDateTime) * 60) OUTPUT: Expand|Select|Wrap|Line Numbers Difference in Hours: 4 Difference in Minutes: 15 May 20 '07 #2

 P: 4 Adezii, Thanks for the tip! I tried it and it works but my problem is combining the hours and minutes (HH:NN) in records. ex: 4:05, 12:14, etc I tried to divide the total minutes by 60 but I don't get a remainer. The application is, the time is entered in severalrecords for a job, then I want to be able to sum the hours and minutes from the records and get total time (hours:minutes) for the job. Thank again I'll illustrate 1 Method by code: Expand|Select|Wrap|Line Numbers Dim dteStartDateTime As Date, dteEndDateTime As Date dteStartDateTime = #5/20/2007 7:00:00 AM# dteEndDateTime = #5/20/2007 11:15:00 AM#   Debug.Print "Difference in Hours: " & DateDiff("h", dteStartDateTime, dteEndDateTime) Debug.Print "Difference in Minutes: " & DateDiff("n", dteStartDateTime, dteEndDateTime) - _             (DateDiff("h", dteStartDateTime, dteEndDateTime) * 60) OUTPUT: Expand|Select|Wrap|Line Numbers Difference in Hours: 4 Difference in Minutes: 15 May 21 '07 #3

 Expert 2.5K+ P: 3,532 So after calculating the two, you need to concatenate them: Expand|Select|Wrap|Line Numbers TimeDifference =  [Difference in Hours] & ":" & [Difference in Minutes] May 21 '07 #4

 Expert 5K+ P: 8,619 Adezii, Thanks for the tip! I tried it and it works but my problem is combining the hours and minutes (HH:NN) in records. ex: 4:05, 12:14, etc I tried to divide the total minutes by 60 but I don't get a remainer. The application is, the time is entered in severalrecords for a job, then I want to be able to sum the hours and minutes from the records and get total time (hours:minutes) for the job. Thank again missingling has the correct response about concatenating both differences. Look at the code again, you are not dividing by 60. To get the Minutes component you are calculating the total number of minutes difference - (minus) the number of Hours difference * 60. May 21 '07 #5

 P: 3 missingling has the correct response about concatenating both differences. Look at the code again, you are not dividing by 60. To get the Minutes component you are calculating the total number of minutes difference - (minus) the number of Hours difference * 60. Query will be like as :------------ db2 "select substr(HOSTNAME,1,25) as hostname,ltrim(rtrim(char(timestampdiff(8,char(tim estamp(CLOSETIME)-timestamp(STARTTIME)))))) ||':' || char((int( ltrim(rtrim(char(timestampdiff(4,char(timestamp(CL OSETIME)-timestamp(STARTTIME))))))))- ( int( ltrim(rtrim(char(timestampdiff(8,char (timestamp(CLOSETIME)-timestamp(STARTTIME)))))))*60)) as difftime from EVENTS_TIME" May 29 '07 #6

 P: 41 plese will u post this code in c# plssssssssssssssssss Feb 27 '10 #7

 Expert 5K+ P: 8,619 This sounds like a question for the C# Forum, not here. Feb 27 '10 #8