0

I wanted to make a duration calculation in a batch file and hav a problem with the modulo calculation. Examples on Websites always show one digit calculations like 14%%3 --> 2. To do a Modulo calculation with 60 or 3600 was always rejected. So I tried first a division and a modullo %%1 over the answer variable. But this was also denied. Anybody an idea how to proceed with situations like that?

Here is the code snipit:

set endzeit=Ende: %date% %time:~-11,2%:%time:~-8,2%:%time:~-5,2%
set /a ende=(%time:~-11,2%)*3600 + (%time:~-8,2%)*60 + (%time:~-5,2%)
set /a dauer=%ende%-%anfang%
set /a dauer_r=(%dauer%/3600)%%1
set /a dauer_sek=(%dauer_std_r%/60)%%1
set /a dauer_min=(%dauer_std_r%-%dauer_sek%)/60
set /a dauer_std=(%dauer%-%dauer_r%)/3600
echo %dauer_std%:%dauer_min%:%dauer_sek%
Manolo
  • 7
  • 3
  • what do you mean by "denied" and "rejected"? – phuclv Jan 04 '18 at 08:07
  • `modulo 1` doesn't make any sense. `%anfang%` seems not to be defined, so some variables remain empty, which leads to syntax errors. Run with `echo on` and watch, what actually gets executed. Note: Numbers starting with a zero are interpreted as octal, which means, `08` and `09` will give you an error too. – Stephan Jan 04 '18 at 08:10
  • %anfang% is defined the same as %endzeit% just at the begining of the batch file. %%1 was just an idea to get the reminder out of the division. The error I get is: Fehlender Operand Fehlender Operand 0:: – Manolo Jan 04 '18 at 08:14
  • set endzeit=Ende: 04.01.2018 9:16:18 set /a ende=( 9)*3600 + (16)*60 + (18) set /a dauer=33378-33370 set /a dauer_r=(8/3600)%1 set /a dauer_sek=(/60)%1 Fehlender Operand set /a dauer_min=(-)/60 Fehlender Operand set /a dauer_std=(8-0)/3600 echo 0:: 0:: – Manolo Jan 04 '18 at 08:19
  • ok I see... there are divisions with 0 – Manolo Jan 04 '18 at 08:23
  • and `set /a dauer_sek=(/60)%1 Fehlender Operand ` didn't make you think, why `%dauer_std_r%` is empty? – Stephan Jan 04 '18 at 08:23
  • Yes now I get it... Thanks – Manolo Jan 04 '18 at 08:25
  • I suggest you to review [this answer](https://stackoverflow.com/questions/42603119/arithmetic-operations-with-hhmmss-times-in-batch-file/42603985#42603985) – Aacini Jan 04 '18 at 14:51

2 Answers2

0

Your time calculation is a bit complicated, better start with hours.
set /a doesn't need the variables to be in % (works, but makes it harder to read)
Added code to process minutes/seconds 08 and 09 correctly (by adding a 1 at the start and subtracting 100)

setlocal
set anfang=20000
set endzeit=Ende: %date% %time:~-11,2%:%time:~-8,2%:%time:~-5,2%
set /a ende=(%time:~-11,2%)*3600 + (1%time:~-8,2% - 100)*60 + (1%time:~-5,2% - 100)
set /a dauer=ende - anfang

set /a dauer_std=dauer / 3600
set /a dauer_min=(dauer - dauer_std*3600) / 60
set /a dauer_sek=dauer - dauer_std*3600 - dauer_min*60
REM shortcut: set /a dauer_sek=dauer %%60

echo %dauer_std%:%dauer_min%:%dauer_sek% 

(Note: as you are probably aware, you'll get wrong results when the time crosses midnight)

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

Here is an example which leverages PowerShell for the duration calculation.

@Echo Off
Echo Gestartet
Set "anfang=%DATE% %TIME%"
Echo Warten
Timeout 28 /NoBreak>Nul
Echo Fertig
Set "endzeit=%DATE% %TIME%"
Echo Berechnen
For /F "Delims=" %%A In ('PowerShell -C^
 "([DateTime]'%endzeit%'-[DateTime]'%anfang%').ToString()"') Do Echo %%~nA
Pause

Replace lines 4 & 5 with your actual required command(s)
You may need to change %TIME% to %TIME:,=.% to make it work with a German Windows.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    This does not answer the question or at least contains parts which are not related to the question. OP does not want any waiting, just calculation. – Melebius Jan 04 '18 at 09:56
  • @Melebius it's just additional code to make it work. It does solve OP's problem in a different way, which is perfectly valid (that was a X-Y-Problem). Change `%TIME%` to `%TIME:,=.%` to make it work with a German windows too. – Stephan Jan 04 '18 at 10:19
  • 1
    @Stephan I understand but this assumption should be included in the answer. (Have a look at https://meta.stackexchange.com/q/114762/217657 or https://meta.stackoverflow.com/q/300837/711006.) – Melebius Jan 04 '18 at 10:36
  • 1
    @Melebius, the OP provided incomplete code which required additions in order to test/check. As Stephan had already provided code showing a set of relatively complex math operations, I decided to post something which does the calculation in a more friendly manner. I could have just supplied incomplete code, as did the OP, but decided to add some translated echo's in order to help them test it independently of their own script. I'm not here to post paragraphs of tutorial in explanation, I posted a piece of code which I expected would be easy for the OP to break down to it's relevant components. – Compo Jan 04 '18 at 14:17