By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 852 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
7 Replies


ADezii
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
  1. Dim dteStartDateTime As Date, dteEndDateTime As Date
  2. dteStartDateTime = #5/20/2007 7:00:00 AM#
  3. dteEndDateTime = #5/20/2007 11:15:00 AM#
  4.  
  5. Debug.Print "Difference in Hours: " & DateDiff("h", dteStartDateTime, dteEndDateTime)
  6. Debug.Print "Difference in Minutes: " & DateDiff("n", dteStartDateTime, dteEndDateTime) - _
  7.             (DateDiff("h", dteStartDateTime, dteEndDateTime) * 60)
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Difference in Hours: 4
  2. 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
  1. Dim dteStartDateTime As Date, dteEndDateTime As Date
  2. dteStartDateTime = #5/20/2007 7:00:00 AM#
  3. dteEndDateTime = #5/20/2007 11:15:00 AM#
  4.  
  5. Debug.Print "Difference in Hours: " & DateDiff("h", dteStartDateTime, dteEndDateTime)
  6. Debug.Print "Difference in Minutes: " & DateDiff("n", dteStartDateTime, dteEndDateTime) - _
  7.             (DateDiff("h", dteStartDateTime, dteEndDateTime) * 60)
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Difference in Hours: 4
  2. Difference in Minutes: 15
May 21 '07 #3

missinglinq
Expert 2.5K+
P: 3,532
So after calculating the two, you need to concatenate them:

Expand|Select|Wrap|Line Numbers
  1. TimeDifference =  [Difference in Hours] & ":" & [Difference in Minutes]
May 21 '07 #4

ADezii
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
  1. missingling has the correct response about concatenating both differences.
  2. 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
  1. missingling has the correct response about concatenating both differences.
  2. 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

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

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

Post your reply

Sign in to post your reply or Sign up for a free account.