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

Posted on 2004-11-24
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
    LVL 2

    Accepted Solution

    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")
    LVL 6

    Expert Comment

    Try using DateDiff ...

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

    Author Comment

    Thanks a lot!!!

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    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…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    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…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now