Working out time difference between 2 times

i have this code, but it doesnt seem to work out time difference correctly

when changing values in line  initialTime=“18:40:00”

can somebody please help

function GetTimeDifference(intialTime,finalTime)
    initialHour=tonumber(string.sub(intialTime,1,2)) *3600
    initialMinute=tonumber(string.sub(intialTime,4,5))*60
    initialSecond=tonumber(string.sub(intialTime,7,8))

    finalHour=tonumber(string.sub(finalTime,1,2))*3600
    finalMinute=tonumber(string.sub(finalTime,4,5))*60
    finalSecond=tonumber(string.sub(finalTime,7,8))

    totalInitialTime=initialHour+initialMinute+initialSecond
    totalFinalTime=finalHour+finalMinute+finalSecond
    local duration=totalFinalTime-totalInitialTime

    formatedDuration=“00:00:00”
    if(duration<10) then
        formatedDuration=“00:00:0”…duration
    elseif(duration>9 and duration<60) then
        formatedDuration=“00:00:”…duration
    elseif(duration>59 and duration<=3600 ) then
        --minutes handler
        intermediateCalc=(duration/60)
        i,j=string.find(tostring(intermediateCalc),".")
        if(i==nil and j==nil) then
          formatedDuration=“00:0”…intermediateCalc
        else
           min=string.sub(tostring(intermediateCalc),i,j)
           if(tonumber(min)<10) then
            formatedDuration=“00:0”…min
           else
              formatedDuration=“00:”…min
          end
        end

        newSeconds=duration%60
        if(newSeconds<10) then
            formatedDuration=formatedDuration…":0"
                    …newSeconds
        else
            formatedDuration=formatedDuration…":"
                 …newSeconds
        end
    else
        --hour handler

        newMinutes=(finalMinute-initialMinute)/60
        if(newMinutes<0) then
          newMinutes=newMinutes*-1
        end

        if(newMinutes<10) then
            newMinutes=“0”…newMinutes
        end

        newSeconds=(finalSecond-initialSecond)
        if(newSeconds<0) then
          newSeconds=newSeconds*-1
        end

        if(newSeconds<10) then
            newSeconds=“0”…newSeconds
        end

        formatedDuration=(finalHour-initialHour)/3600
          …":"…newMinutes…":"…newSeconds
    end
    return formatedDuration
end

    initialTime=“18:40:00”
    finalTime=os.date("%H:%M:%S")
    print("")
    print (“The Initial Time is “…initialTime)
    print(””)
    print (“The Current Time is “…finalTime)
    print(””)
    duration=GetTimeDifference(initialTime,finalTime)
    print (“The Time Difference is “…duration)
    print(””)

Not sure what exactly the error is you’re looking for, without a concrete sample.

But one issue you have for sure is, you don’t handle negative time differences.

I.e. if the current system time is before your final time you’ll always end with the result of your first if, i.e. 00.00.0 + the negative number in seconds between your two times.

You may have to decide if you always want the positive difference and if so, work around that situation (by adding 86400 if the time is negative), I’ve added this as a commented part

Last but not least, you implemented it much more complicated than requried, I’ve changed the code into a much simpler version

[lua]

function GetTimeDifference( intialTime, finalTime )

    local initialHour=tonumber(string.sub(intialTime,1,2)) *3600

    local initialMinute=tonumber(string.sub(intialTime,4,5))*60

    local initialSecond=tonumber(string.sub(intialTime,7,8))

    local finalHour=tonumber(string.sub(finalTime,1,2))*3600

    local finalMinute=tonumber(string.sub(finalTime,4,5))*60

    local finalSecond=tonumber(string.sub(finalTime,7,8))

    local totalInitialTime=initialHour+initialMinute+initialSecond

    local totalFinalTime=finalHour+finalMinute+finalSecond

    local duration=totalFinalTime-totalInitialTime

    – uncomment these lines if you always want the positive difference even if it means it’s on the next day

    --if duration < 0 then

    –    duration = duration + 86400

    --end

    local durationSeconds = duration % 60

    duration = math.floor( duration / 60 )

    local durationMinutes = duration % 60

    local durationHours = math.floor( duration / 60 )

    return string.format( “%02d.%02d.%02d”, durationHours, durationMinutes, durationSeconds )

end

initialTime=“18:40:00”

finalTime=os.date("%H:%M:%S")

print("")

print ("The Initial Time is "…initialTime)

print("")

print ("The Current Time is "…finalTime)

print("")

duration=GetTimeDifference(initialTime,finalTime)

print ("The Time Difference is "…duration)

print("")

[/lua]

Hi

new code is looking good

would it be possible to include day as well in above code

this would be very kind of you

thanks so much

Not sure what exactly the error is you’re looking for, without a concrete sample.

But one issue you have for sure is, you don’t handle negative time differences.

I.e. if the current system time is before your final time you’ll always end with the result of your first if, i.e. 00.00.0 + the negative number in seconds between your two times.

You may have to decide if you always want the positive difference and if so, work around that situation (by adding 86400 if the time is negative), I’ve added this as a commented part

Last but not least, you implemented it much more complicated than requried, I’ve changed the code into a much simpler version

[lua]

function GetTimeDifference( intialTime, finalTime )

    local initialHour=tonumber(string.sub(intialTime,1,2)) *3600

    local initialMinute=tonumber(string.sub(intialTime,4,5))*60

    local initialSecond=tonumber(string.sub(intialTime,7,8))

    local finalHour=tonumber(string.sub(finalTime,1,2))*3600

    local finalMinute=tonumber(string.sub(finalTime,4,5))*60

    local finalSecond=tonumber(string.sub(finalTime,7,8))

    local totalInitialTime=initialHour+initialMinute+initialSecond

    local totalFinalTime=finalHour+finalMinute+finalSecond

    local duration=totalFinalTime-totalInitialTime

    – uncomment these lines if you always want the positive difference even if it means it’s on the next day

    --if duration < 0 then

    –    duration = duration + 86400

    --end

    local durationSeconds = duration % 60

    duration = math.floor( duration / 60 )

    local durationMinutes = duration % 60

    local durationHours = math.floor( duration / 60 )

    return string.format( “%02d.%02d.%02d”, durationHours, durationMinutes, durationSeconds )

end

initialTime=“18:40:00”

finalTime=os.date("%H:%M:%S")

print("")

print ("The Initial Time is "…initialTime)

print("")

print ("The Current Time is "…finalTime)

print("")

duration=GetTimeDifference(initialTime,finalTime)

print ("The Time Difference is "…duration)

print("")

[/lua]

Hi

new code is looking good

would it be possible to include day as well in above code

this would be very kind of you

thanks so much