hh:mm:ss datediff format issue

Drstein99

Junior Contributor
Joined
Sep 26, 2003
Messages
283
Location
Audubon, Nj
Trying to get a simple "HH:MM:SS" (in STRING format)

from the difference of these two datetime formats:

endtime-starttime


I want to know in HH:MM:SS how much time inbetween starttime and enddtime.



I know:

DateDiff(DateInterval.Second, starttime, endTime)

will return the number of seconds, but its the total number of seconds, not the ones leftover from hours and minutes. Please help.
 
If you use the proper CLR-compliant method (DateTime structure), you can do it something like this:

Visual Basic:
        Dim date1 As DateTime = DateTime.Parse("10:00:00")
        Dim date2 As DateTime = DateTime.Parse("11:00:00")
        Dim diff As TimeSpan = date2.Subtract(date1)

        MessageBox.Show(diff.Hours.ToString())
It should show a one hour different between the two.

As always, consult the MSDN for more info.
 
Ok that wasn't the exact thing I was looking for but it pointed me right and I now have the answer. Thanks for your help case closed.


messagebox.show(diff.Hours.ToString & ":" & diff.Minutes.ToString & ":" & diff.Seconds.ToString)
 
I think it will be better formatted if you use diff.ToString and it will return a string like you wanted. However, with your, you could get a time like 6:7:2, whereas with diff.ToString it would be 6:07:02.
 
Perfect! I'm going to be doing alot of time tracking etc... And I'm gonna need this function all over the place. Thank you very much. I'm starting to respect .net better every day because of this forum. With the standard help that is offered with the package I get so lost!
 
Back
Top