Page 1 of 1

[SOLVED] Make Minutes & Seconds output 2 Digits

Posted: 20 May 2023 03:46
by PAB
Good morning,

It has been a LONG time since I was here last due to health issues and I have a problem which I just can't seem to solve.

The below works great, except I would like the Minutes & Seconds to show as 2 digits instead of one. The program will run no longer than say 15 minutes so Hours are NOT an issue.

Code: Select all

@echo off

setlocal EnableDelayedExpansion

for /f "skip=1 tokens=*" %%i in ('"PowerShell Get-Date"') do (set "[ST]=%%i")

... CODE HERE ...

Call :Timer

echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit

:Timer

for /f "skip=1 tokens=*" %%i in ('"PowerShell Get-Date"') do (set "[ET]=%%i")
set [T]=PowerShell -Command "&{$ST = [DateTime]::Parse('%[ST]%'); $ET = [DateTime]::Parse('%[ET]%'); echo (-Join( $D = ($ET - $ST).TotalSeconds));}"
for /f "delims=" %%i in ('%[T]%') do (set "[T]=%%i")
set /a [Minutes]=%[T]% / 60
set /a [Seconds]=%[T]%-%[Minutes]% * 60
echo Runtime: %[Minutes]% Minutes - %[Seconds]% Seconds
Exit /b
Stay SAFE, Best Regards, Paul.

Re: Make Minutes & Seconds output 2 Digits

Posted: 21 May 2023 08:47
by jeb
You could prefix the values with "0" and take only the last two characters.

Code: Select all

set /a [Minutes]=%[T]% / 60
set "min=0%[minutes]%"
set "min=%min:~-2%"
echo %min_%

Re: Make Minutes & Seconds output 2 Digits

Posted: 21 May 2023 11:59
by PAB
jeb wrote:
21 May 2023 08:47
You could prefix the values with "0" and take only the last two characters.

Code: Select all

set /a [Minutes]=%[T]% / 60
set "min=0%[minutes]%"
set "min=%min:~-2%"
echo %min_%
Thanks jeb. I applied the method to the below but unfortunately it doesn't produce ANY results. I just used [Get-WinUserLanguageList] for testing purposes.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /f "skip=1 tokens=*" %%i in ('"PowerShell Get-Date"') do (set "[ST]=%%i")

PowerShell Get-WinUserLanguageList  ^| Sort-Object -Property LanguageTag ^| Format-Table -AutoSize
PowerShell Get-WinUserLanguageList  ^| Sort-Object -Property LanguageTag ^| Format-Table -AutoSize
PowerShell Get-WinUserLanguageList  ^| Sort-Object -Property LanguageTag ^| Format-Table -AutoSize

Call :Timer
echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit

:Timer

for /f "skip=1 tokens=*" %%i in ('"PowerShell Get-Date"') do (set "[ET]=%%i")
set [T]=PowerShell -Command "&{$ST = [DateTime]::Parse('%[ST]%'); $ET = [DateTime]::Parse('%[ET]%'); echo (-Join( $D = ($ET - $ST).TotalSeconds));}"
for /f "delims=" %%i in ('%[T]%') do (set "[T]=%%i")
set /a [Minutes]=%[T]% / 60
set "min=0%[Minutes]%"
set "min=%min:~-2%"
set /a [Seconds]=%[T]%-%[Minutes]% * 60
set "sec=0%[Seconds]%"
set "sec=%sec:~-2%"
echo Runtime: %min_% Minutes - %sec_% Seconds
Exit /b

Re: Make Minutes & Seconds output 2 Digits

Posted: 21 May 2023 14:09
by jeb
Hi PAB,

I just add an underscore, which destroys all.

It should be

Code: Select all

echo %min%

Re: Make Minutes & Seconds output 2 Digits

Posted: 22 May 2023 12:00
by PAB
Thanks jeb, that worked very nicely.

I managed to adapt and apply that to Part 1 of my Script, then to Part 2 of my Script, and then to add Part 1 & Part 2 together to come up with a total runtime for the whole Script.

Thanks again for your time, input, and explanations, it is very much appreciated.