[SOLVED] Make Minutes & Seconds output 2 Digits

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

[SOLVED] Make Minutes & Seconds output 2 Digits

#1 Post by PAB » 20 May 2023 03:46

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.
Last edited by PAB on 22 May 2023 12:01, edited 1 time in total.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Make Minutes & Seconds output 2 Digits

#2 Post by jeb » 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_%

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Make Minutes & Seconds output 2 Digits

#3 Post by PAB » 21 May 2023 11:59

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Make Minutes & Seconds output 2 Digits

#4 Post by jeb » 21 May 2023 14:09

Hi PAB,

I just add an underscore, which destroys all.

It should be

Code: Select all

echo %min%

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Make Minutes & Seconds output 2 Digits

#5 Post by PAB » 22 May 2023 12:00

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.

Post Reply