[Last Call] Learn how to a build a cloud-first strategyRegister Now


calculate total hours and minutes between two times but .....

Posted on 2004-11-24
Medium Priority
Last Modified: 2008-02-26
I use this code...
Timediff = Format(CDate(TimeNow) - CDate(TimeFirst), "Long Time")

but if i change windows clock to the next day, for example:

now = 25-nov-2004 11:07:15
first = 24-nov-2004 11:00:00

i want to get this timediff:


no ....

can you help me?
Question by:jorgeani

Accepted Solution

craigewens earned 200 total points
ID: 12663733
I use this function when calculating such things...

Public Function TimeDiff(LesserTime As String, LesserTimeDate As String, GreaterTime As String, GreaterTimeDate As String) As String
Dim Thrs As Single, Tmins As Single, Tsecs As Single, ReturnTime As String
Dim DTcheck As Single
Dim i, Var_tmp_Hour As Integer

DTcheck = DateDiff("d", LesserTimeDate, GreaterTimeDate)

Var_tmp_Hour = Hour(GreaterTime)
If DTcheck > 0 Then
  For i = 1 To DTcheck
    Var_tmp_Hour = Var_tmp_Hour + 24
 ElseIf DTcheck < 0 Then
  TimeDiff = "ERROR"
  Exit Function
End If
Thrs = Var_tmp_Hour - Hour(LesserTime)

If Thrs > 0 Then
  Tmins = Minute(GreaterTime) - Minute(LesserTime)
  If Tmins < 0 Then
    Tmins = Tmins + 60
    Thrs = Thrs - 1
  End If
 ElseIf Thrs = 0 And Minute(GreaterTime) > Minute(LesserTime) Then
   Tmins = Minute(GreaterTime) - Minute(LesserTime)
   TimeDiff = "ERROR"
   Exit Function
End If
Tsecs = Second(GreaterTime) - Second(LesserTime)
If Tsecs < 0 Then
  Tsecs = Tsecs + 60
  If Tmins > 0 Then Tmins = Tmins - 1 Else Tmins = 59
End If
TimeDiff = Thrs & ":" & Tmins & ":" & Tsecs
End Function

To use it just call something like this:

MsgBox TimeDiff("11:00:00", "24/11/2004", "11:07:15", "25/11/2004")

Expert Comment

ID: 12663783
Try using DateDiff ...

DateDiff("n",#24/11/2004 23.30#,#25/11/2004 23.35#)/60.0

Author Comment

ID: 12664173
Thanks a lot!!!

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question