Discussion:
24-hour format to 12-hour format using one equation
(too old to reply)
Tom Del Rosso
2020-12-06 01:50:10 UTC
Permalink
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
2020-12-06 02:33:47 UTC
Permalink
Post by Tom Del Rosso
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
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
2020-12-06 03:11:29 UTC
Permalink
Post by Zaidy036
Post by Tom Del Rosso
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
use Net Time https://ss64.com/nt/net-time.html
FOR /F "tokens=7 delims= " %%n IN ('NET TIME \\PCname') DO SET _TX=%%n
That will take even longer to execute.
Zaidy036
2020-12-06 03:27:37 UTC
Permalink
Post by Tom Del Rosso
Post by Zaidy036
Post by Tom Del Rosso
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
use Net Time https://ss64.com/nt/net-time.html
FOR /F "tokens=7 delims= " %%n IN ('NET TIME \\PCname') DO SET _TX=%%n
That will take even longer to execute.
if a millisecond or two is that important have fun
Tom Del Rosso
2020-12-06 05:22:35 UTC
Permalink
Post by Zaidy036
Post by Tom Del Rosso
Post by Zaidy036
Post by Tom Del Rosso
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
use Net Time https://ss64.com/nt/net-time.html
FOR /F "tokens=7 delims= " %%n IN ('NET TIME \\PCname') DO SET _TX=%%n
That will take even longer to execute.
if a millisecond or two is that important have fun
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
2020-12-06 05:27:52 UTC
Permalink
Post by Tom Del Rosso
Post by Zaidy036
Post by Tom Del Rosso
Post by Zaidy036
Post by Tom Del Rosso
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
use Net Time https://ss64.com/nt/net-time.html
FOR /F "tokens=7 delims= " %%n IN ('NET TIME \\PCname') DO SET _TX=%%n
That will take even longer to execute.
if a millisecond or two is that important have fun
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.
Correction, it doesn't depend on the time service, but the other points
are valid.
Zaidy036
2020-12-06 19:20:49 UTC
Permalink
Post by Tom Del Rosso
Post by Zaidy036
Post by Tom Del Rosso
Post by Zaidy036
Post by Tom Del Rosso
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
use Net Time https://ss64.com/nt/net-time.html
FOR /F "tokens=7 delims= " %%n IN ('NET TIME \\PCname') DO SET _TX=%%n
That will take even longer to execute.
if a millisecond or two is that important have fun
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.
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
2020-12-06 19:31:26 UTC
Permalink
Post by Tom Del Rosso
Post by Zaidy036
Post by Tom Del Rosso
Post by Zaidy036
Post by Tom Del Rosso
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
use Net Time https://ss64.com/nt/net-time.html
FOR /F "tokens=7 delims= " %%n IN ('NET TIME \\PCname') DO SET _TX=%%n
That will take even longer to execute.
if a millisecond or two is that important have fun
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.
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
JJ
2020-12-06 09:58:48 UTC
Permalink
Post by Tom Del Rosso
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
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%
Herbert Kleebauer
2020-12-06 22:12:17 UTC
Permalink
Post by Tom Del Rosso
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
2021-01-24 16:47:26 UTC
Permalink
Post by Tom Del Rosso
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
--
@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
2021-01-24 17:34:22 UTC
Permalink
Post by Tom Del Rosso
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
--
@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
2021-01-24 18:03:44 UTC
Permalink
Post by Tom Del Rosso
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
--
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

Loading...