Can someone fix this time subtractor to return negative value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samvb
    New Member
    • Oct 2006
    • 228

    Can someone fix this time subtractor to return negative value

    this is the code which I found online and works great. I forgot its author but whoever you are, 10x but a lil help would be appreciated...

    here is the code

    Code:
    'public variables
    Const MSecInSec As Double = 1000
    Const SecsInMin As Double = 60
    Const MinsInHour As Double = 60
    Const HoursInDay As Double = 24
    Const SecsInDay As Double = HoursInDay * MinsInHour * SecsInMin
    Const mSecMult As Double = 1 / (SecsInDay * MSecInSec)
    
    private sub command1_click()
    
    msgbox  ReturnTimeDifference("01:00:00:00","04:30:00:00")
    msgbox  ReturnTimeDifference("04:00:00:00","01:30:00:00")
    
    
    end sub
    
    
    Public Function ReturnTimeDifference(ByVal smalltime As String, ByVal bigtime As String) As String
    Dim TimeA As Date
    Dim TimeB As Date
     Dim Res As Double
     Dim timeparts As Variant
     Dim timepartsb As Variant
     Dim ms As String
     Dim ms2 As String
     timeparts = Split(smalltime, ":")
     timepartsb = Split(bigtime, ":")
     ms = timeparts(3)
     ms2 = timepartsb(3)
     
    ms = ms & String(3 - Len(ms), "0")
    ms2 = ms2 & String(3 - Len(ms2), "0")
     
    
    
     
     TimeA = TimeSerialEx(timeparts(0), timeparts(1), timeparts(2), ms) '01:04:12.2
     TimeB = TimeSerialEx(timepartsb(0), timepartsb(1), timepartsb(2), ms2) '05:08:15.55
    
     Res = Format$((TimeB - TimeA) * SecsInDay, "0.00") 'RETURNS POSITIVE NO MATTER WHAT!
    
    ReturnTimeDifference = Format$(Res / SecsInDay, "hh:nn:ss:") & _
     Round(Res - Fix(Res), 2) * 100 'avoids the decimal point
    
    End Function
    both return positive value. he had commented this line as "returns postive no matter what

    Code:
     Res = Format$((TimeB - TimeA) * SecsInDay, "0.00") 'RETURNS POSITIVE NO MATTER WHAT!
    but i can't seem to figure out which part of it makes Res positive always.

    any tips?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    The format 0:00 makes it return only positive numbers since it doesn't allow for the negative sign.

    Comment

    • samvb
      New Member
      • Oct 2006
      • 228

      #3
      i thot about it and removed it and tweaked it but nothing changed. can u help me out a lil bit?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        I don't know the formats it allows. You may have to look that up but you can try 0:00;-0:00.

        Comment

        • MikeTheBike
          Recognized Expert Contributor
          • Jun 2007
          • 640

          #5
          Hi

          As pointed out by Rabbit the Time format does not take account of the sign and only returns +ve values.
          However, contrary to the statement in the code this line
          Code:
          Res = Format$((TimeB - TimeA) * SecsInDay, "0.00")
          does return -ve values, so the only suggestion I have is adding this line to the bottom of the function
          Code:
          If Res < 1 Then ReturnTimeDifference = "-" & ReturnTimeDifference
          ??

          MTB

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            As JosAH has pointed out somewhere you may wish to research JD (for Julian day) where you can convert a date to JD then add (or subtract) the number of days and then convert the new JD back to date. This is simply done.

            Comment

            Working...