Difference between two dates durations
Lets say I have a date and time that is 05-01-07 01:11:11 pm. I have another date and time that is 05-05-07 05:50:55 pm.
How can I find the duration between both dates and times down to the exact seconds?
I would like to get how many days in between and the remaining of hours, minutes and seconds.
So, in this case it would be 4 days and something hour, minutes and seconds.
Thank You
Re: Difference between two dates durations
DateDiff() should do the trick.
Do a search on this forum for more information.
Re: Difference between two dates durations
The DateDiff() Function should work.
Re: Difference between two dates durations
just for a quick reference
vb Code:
Private Sub Command1_Click()
Dim myDate1 As Date
myDate1 = "01-05-07 01:11:11 pm"
Dim myDate2 As Date
myDate2 = "05-05-07 05:50:55 pm"
MsgBox DateDiff("d", myDate1, myDate2)
MsgBox DateDiff("h", myDate1, myDate2)
MsgBox DateDiff("N", myDate1, myDate2)
MsgBox DateDiff("S", myDate1, myDate2)
End Sub
Re: Difference between two dates durations
Thanks guys!
But this could not be right.
Code:
Dim myDate1 As Date
myDate1 = "01-05-07 01:11:11 pm"
Dim myDate2 As Date
myDate2 = "05-05-07 05:50:55 pm"
Dim strDifference As String
strDifference = DateDiff("d", myDate1, myDate2) & " days - " & _
DateDiff("h", myDate1, myDate2) & " hours - " & _
DateDiff("n", myDate1, myDate2) & " minutes - " & _
DateDiff("s", myDate1, myDate2) & " seconds."
MsgBox "The difference between mydate2 and mydate1 is: " & strDifference
End
Re: Difference between two dates durations
Oh, yes it could be and is..... but it's being misused..... I'm guessing the days part is probably OK.... but I'm betting that you took one look at the hours minutes and seconds and went WOAH!.... but it's right..... For instance, if you look at the datediff between "5/2/2007" and "5/3/2007" using days... you get 1. If you use the same dates, but look at hours.... you get 24... both are correct. If you look at minutes you get (24*60).... all of which are correct.... Now, if you want 5 days, 3 hours, 27 minutes and 6 seconds..... it gets a little more complicated.
First get the number of days.... then get the number of total hours, then subtract from that (days * 24) ..... what's left will get you the hours.... and so on and so on down through seconds. Like I said it gets complicated realy fast.
-tg
Re: Difference between two dates durations
you can do it like this
Code:
Const sFormat As String = " \da\y\s hh \hour\s mm \mi\nute\s ss \se\co\n\d\s"
Dim d1 As Date, d2 As Date
d1 = #5/1/2007 1:11:11 PM#
d2 = #5/5/2007 5:50:55 PM#
Debug.Print Int(d2 - d1) & Format$(d2 - d1, sFormat)