Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

24-hour format to 12-hour format using one equation

42 views
Skip to first unread message

Tom Del Rosso

unread,
Dec 5, 2020, 8:50:03 PM12/5/20
to
The last line is the thing I want to point out. It's a single equation
that converts the hour from 24-hour format to 12-hour format. I'm sure
this would run faster than an IF statement and more so if the IF was
followed by a GOTO. In the past I have used IF because I hadn't thought
of doing it all in arithmetic. But I don't see a way to avoid the IF
that chooses am and pm.

set tm=%time%
set hr=%tm:~0,2%
if %hr% LSS 12 (set AMPM=am) else (set AMPM=pm)
set /a hr=hr%%12 + !hr*12 + !(hr-12)*12

--



Zaidy036

unread,
Dec 5, 2020, 9:33:49 PM12/5/20
to
use Net Time https://ss64.com/nt/net-time.html

FOR /F "tokens=7 delims= " %%n IN ('NET TIME \\PCname') DO SET _TX=%%n

Tom Del Rosso

unread,
Dec 5, 2020, 10:11:17 PM12/5/20
to
That will take even longer to execute.



Zaidy036

unread,
Dec 5, 2020, 10:27:38 PM12/5/20
to
if a millisecond or two is that important have fun

Tom Del Rosso

unread,
Dec 6, 2020, 12:22:35 AM12/6/20
to
It's more than a millisecond to run an external program with the FOR
command. It depends on a service running, and external commands are much
more likely to have changes in syntax and also in output format between
different OS versions.



Tom Del Rosso

unread,
Dec 6, 2020, 12:28:23 AM12/6/20
to
Correction, it doesn't depend on the time service, but the other points
are valid.



JJ

unread,
Dec 6, 2020, 4:58:51 AM12/6/20
to
You can use the ! operator trick like you did for the hour number conversion
part, but it'll need more commands which makes it slower than a single IF.
e.g.

@echo off
setlocal
set tm=%time%
set hr=%tm:~0,2%

set /a hr2=hr%%12 + !hr*12 + !(hr-12)*12

set /a i=hr/12
rem i: 0=am, 1=pm
set AMPM=ap
call set AMPM=%%AMPM:~%i%,1%%m

echo %tm% = %hr2%%AMPM%

Zaidy036

unread,
Dec 6, 2020, 2:20:51 PM12/6/20
to
I have not timed either method but NET TIME will operate on the computer
it is running on and does not need another one to get the time. On my
Win 10 Pro %Time% is in 24 hour and NET TIME is 12 hour format.

Use SET hr=%TIME:~0,2% since you do not appear to use %TIME% again?

And MOD may be faster:
SET AMPM=AM
SET /A HR=%TIME:~0,2% % 12
IF HR GTR 0 SET AMPM=PM

Zaidy036

unread,
Dec 6, 2020, 2:31:29 PM12/6/20
to

Herbert Kleebauer

unread,
Dec 6, 2020, 5:12:20 PM12/6/20
to
On 06.12.2020 02:50, Tom Del Rosso wrote:

> of doing it all in arithmetic. But I don't see a way to avoid the IF
> that chooses am and pm.
>
> if %hr% LSS 12 (set AMPM=am) else (set AMPM=pm)

I don't think that speed matters in this case or that a solution without
an "IF" is faster, but this should do it:


setlocal enabledelayedexpansion
set /a n=hr/12*2 & set s=ampm
set AMP=!s:~%n%,2!

mokomoji

unread,
Jan 24, 2021, 11:47:27 AM1/24/21
to
2020년 12월 6일 일요일 오전 10시 50분 3초 UTC+9에 Tom Del Rosso님이 작성한 내용:
@echo off
cd /d %~dp0
setlocal

set z_apm=AM
for /f "eol=0 delims=" %%f in ('set /a "z_t2=%time:~-11,2%/12"') do set z_apm=PM
echo %z_apm% %time:~-11,8%

:end
endlocal
pause

mokomoji

unread,
Jan 24, 2021, 12:34:23 PM1/24/21
to
2020년 12월 6일 일요일 오전 10시 50분 3초 UTC+9에 Tom Del Rosso님이 작성한 내용:
@echo off
cd /d %~dp0
setlocal

for /l %%l in (1,1,23) do (
call set /a "z_t2=%%l/12"
call echo %%z_t2%%|find /i "1" 2>nul>nul&&set z_apm=PM||set z_apm=AM
call echo %%l--%%z_t2%%--%%z_apm%%
)

:end
endlocal
pause

mokomoji

unread,
Jan 24, 2021, 1:03:46 PM1/24/21
to
2020년 12월 6일 일요일 오전 10시 50분 3초 UTC+9에 Tom Del Rosso님이 작성한 내용:
u want and u need..
use for in if...

@echo off
cd /d %~dp0
setlocal

for /l %%l in (0,1,23) do (
call set /a "z_t2=%%l/12"
call echo %%z_t2%%|find /i "1" 2>nul>nul&&set z_apm=PM||set z_apm=AM
call echo %%l--%%z_t2%%--%%z_apm%%
)

set z_apm=AM
for /f "eol=0 delims=" %%f in ('set /a "z_t2=%time:~-11,2%/12"') do set z_apm=PM
echo %z_apm% %time:~-11,8%


for /l %%l in (0,1,23) do (
call set /a "z_t2=%%l/12"
cmd /c if "%%z_t2%%" equ "0" ^(echo AM %%l^) else ^(echo PM %%l^)
)


:end
endlocal
pause




0 new messages