Jump to content

Subtract seconds


Go to solution Solved by jguinch,

Recommended Posts

Please I need to subtract two times and I fail to understand how. 
 
example: I have a start time: 00:00:00 
and a final time: 00:00:10 
 
the final time I subtract 3 seconds or is 00:00:03 
 
so my end result will be the final time -3 seconds 
 
00:00:10 - 00:00:03 = 00:00:07 
 
as I should since I've tried tickstotime timetoticks etc and I fail to deduce how to do it simply. 
 
thank you very much again for your help.
Link to comment
Share on other sites

_DateDiff Possibly.

i don´t think so, because use date start and i´m looking only time

this is the help of _DateDiff

$sStartDate Input Start date in the format "YYYY/MM/DD[ HH:MM:SS]"

$sEndDate Input End date in the format "YYYY/MM/DD[ HH:MM:SS]"

:

Edited by boniakowski
Link to comment
Share on other sites

#include <Date.au3>
#include <MsgBoxConstants.au3>

; subtract 3 seconds from current time
$sNewDate = _DateAdd('s', -3, _NowCalc())
MsgBox($MB_SYSTEMMODAL, "", "Current time -3 seconds: " & $sNewDate)

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Solution

#Include <Date.au3>

$diff1 = _TimeDiff("00:00:10", "00:00:03")
ConsoleWrite($diff1 & @CRLF)


Func _TimeDiff($sTime1, $sTime2)
    Local $iH, $iM, $iS
    If NOT StringRegExp($sTime1 & "-" & $sTime2, "\d\d:\d\d:\d\d-\d\d:\d\d:\d\d") Then Return SetError(1, 0, 0)
        
    Local $iDiff = _DateDiff( "s", "1970/01/01 " & $sTime1, "1970/01/01 " & $sTime2) & 100
    _TicksToTime( Abs($iDiff), $iH, $iM, $iS)
    If @error Then Return SetError(2, 0, 0)
    
    Local $sReturn = StringFormat("%02s", $iH) & ":" & StringFormat("%02s", $iM) & ":" & StringFormat("%02s", $iS)
    If $iDiff > 0 Then $sReturn = "-" & $sReturn
    
    Return $sReturn
EndFunc

Edited by jguinch
Link to comment
Share on other sites

#include <Date.au3>
#include <MsgBoxConstants.au3>

; subtract 3 seconds from current time
$sNewDate = _DateAdd('s', -3, _NowCalc())
MsgBox($MB_SYSTEMMODAL, "", "Current time -3 seconds: " & $sNewDate)

 

Ok your example work fine but always with a date _NowCalc() return date format, i don´t need a date format, i only need a Result of TIME in seconds.

$StartTime=("00:00:00")

$StopTime=("00:00:10")

$Difference=("00:00:03")

$Result=($StopTime-$Difference)

:ermm:

Link to comment
Share on other sites

  • Moderators

Something like this, perhaps? I'm sure a RegEx master will wander by to improve:

#include <Date.au3>
$startTime = _NowTime(5)
$finishTime = StringLeft($startTime, 6) & (StringRight($startTime, 2) - 3)

MsgBox(0, $startTime, $finishTime)

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Ok your example work fine but always with a date _NowCalc() return date format, i don´t need a date format, i only need a Result of TIME in seconds.

$StartTime=("00:00:00")

$StopTime=("00:00:10")

$Difference=("00:00:03")

$Result=($StopTime-$Difference)

:ermm:

It's just a bit of jiggery pokery to get it how you want.

Have a try to make a function which takes hh:mm:ss and returns same.

The function can add yyyy/mm/dd to do the calcs and remove it to return what you need.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

#Include <Date.au3>

$diff1 = _TimeDiff("00:00:10", "00:00:03")
ConsoleWrite($diff1 & @CRLF)


Func _TimeDiff($sTime1, $sTime2)
    Local $iH, $iM, $iS
    If NOT StringRegExp($sTime1 & "-" & $sTime2, "\d\d:\d\d:\d\d-\d\d:\d\d:\d\d") Then Return SetError(1, 0, 0)
        
    Local $iDiff = _DateDiff( "s", "1970/01/01 " & $sTime1, "1970/01/01 " & $sTime2) & 100
    _TicksToTime( Abs($iDiff), $iH, $iM, $iS)
    If @error Then Return SetError(2, 0, 0)
    
    Local $sReturn = StringFormat("%02s", $iH) & ":" & StringFormat("%02s", $iM) & ":" & StringFormat("%02s", $iS)
    If $iDiff > 0 Then $sReturn = "-" & $sReturn
    
    Return $sReturn
EndFunc

It´s i am looking for, thanks for your help.

Thanks too guys: 

JohnOne

 

JLogan3o13

 

yours answers help to open my mind too.

Thank tou very much.

Solved.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...