probleum in calculating the time difference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veer
    New Member
    • Jul 2007
    • 198

    probleum in calculating the time difference

    Hi
    i use Datediff() and it return only hours but i want total time with minutes and seconds at the same time
    i use following code for all this and it works fine but when the the time came which i have mentioned in the coding produces wrong output
    because the difference between is 1:13:52 but it is showing 12:33:52
    how can i correct this please help


    Dim Start As Date
    Dim Finish As Date
    Dim Duration As Date
    Dim outStr As String
    Dim Length As Integer

    'Converting time values to dates
    Start = CDate("5:53:05 PM")
    Finish = CDate("6:26:57 PM")

    'Find duration
    Duration = Finish - Start
    outStr = CStr(Duration)

    'Convert to string an trim AM/PM indicator
    Length = InStr(outStr, "AM")
    outStr = Mid(CStr(Durati on), 1, Length - 2)
    MsgBox (outStr)
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    How the difference is 1:13:52 ?

    I think it is only 00:33:52.

    Comment

    • veer
      New Member
      • Jul 2007
      • 198

      #3
      Hi
      sorry but it is showing 12:33:52
      and i want to show only 00:33:52

      please give some idea

      thanks in advance


      Originally posted by debasisdas
      How the difference is 1:13:52 ?

      I think it is only 00:33:52.

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        i don't understand the following lines of your code

        Length = InStr(outStr, "AM")
        outStr = Mid(CStr(Durati on), 1, Length - 2)

        Comment

        • kadghar
          Recognized Expert Top Contributor
          • Apr 2007
          • 1302

          #5
          Originally posted by debasisdas
          i don't understand the following lines of your code

          Length = InStr(outStr, "AM")
          outStr = Mid(CStr(Durati on), 1, Length - 2)
          Those lines will remove characters from Duration, starting from 1 position before an 'AM' appears for the first time.

          i.e.

          If Duration = "1:22:44 AM"
          then
          outStr = "1:22:44"

          But if the string (Duration) does not have the string 'AM' in it,, then Length (which by the way is an awful name for a variable) will be zero, so the MID function will show an error.

          I dont understand them neither, because the date format might change and Duration wont have 'AM' all times...

          ..so, to the OP, just have in mind the Duration shouldn't be over 12 hours, so you wont have 'AM' in the string. And make sure the format is always a desirable one.

          HTH

          Comment

          • veer
            New Member
            • Jul 2007
            • 198

            #6
            Hi
            thanks for reply
            my duration is always eight hours
            but if you have any sugestion regarding the date format please give

            but my probleum is when the duration is less than one hour , then minutes and seconds are fine but hour should be in 00 format but it is displaying 12 insted of 00

            e.g if duration is like 00:23:52
            but with this code it is like 12:23:52
            just give me suggestion how i will solve this probleum

            i correct my code
            dim Start as date
            dim Finish as date
            dim Duration as date
            dim startdate,endda te as string

            Start = CDate(StartDate )
            Finish = CDate(EndDate)
            Duration = Finish - Start
            msgbox Duration




            Originally posted by kadghar
            Those lines will remove characters from Duration, starting from 1 position before an 'AM' appears for the first time.

            i.e.

            If Duration = "1:22:44 AM"
            then
            outStr = "1:22:44"

            But if the string (Duration) does not have the string 'AM' in it,, then Length (which by the way is an awful name for a variable) will be zero, so the MID function will show an error.

            I dont understand them neither, because the date format might change and Duration wont have 'AM' all times...

            ..so, to the OP, just have in mind the Duration shouldn't be over 12 hours, so you wont have 'AM' in the string. And make sure the format is always a desirable one.

            HTH

            Comment

            • 9815402440
              New Member
              • Oct 2007
              • 180

              #7
              hi

              use following to show duration

              MsgBox Format(Duration , "hh:mm:ss")

              regards
              manpreet singh dhillon hoshiarpur

              Comment

              Working...