So I have come up with this one. What do I have to change?
@echo off
:U
echo ****************** Time Converter! ******************
set /p HOUR= Please Enter the Hours:
set /p MIN= Plese Enter the Minutes:
if %hr% geq 12 (Set timeofday=pm) else (Set timeofday=am)
if %hr% equ 0 (set /a HOUR=%HOUR% 12)
if %hr% gtr 0 (set /a HOUR=%HOUR%)
if %hr% gtr 12 (set /a HOUR=%HOUR%-12)
echo The time in 12hours is %HOUR%:%MIN%%timeofday%
CodePudding user response:
As a courtesy, here's a more robustly coded example of your task, to cater for the many issues your code could have caused. I will leave it for you to review, research, and try to learn from.
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Echo ****************** Time Converter ******************
:GetHours
Set "_Hour="
Set /P "_Hour=Please enter the hour>" || GoTo GetHours
Set _Hour | %SystemRoot%\System32\findstr.exe /RXC:"_Hour=[0123456789]"^
/C:"_Hour=1[0123456789]" /C:"_Hour=2[01234]" 1>NUL || GoTo GetHours
Set "AMPM=AM" & If %_Hour% GEq 12 If %_Hour% LEq 23 Set "AMPM=PM"
If %_Hour% NEq 12 Set /A _Hour %%= 12
:GetMins
Set "_Min="
Set /P "_Min=Enter the number of minutes past the hour>" || GoTo GetMins
Set _Min | %SystemRoot%\System32\findstr.exe /RXC:"_Min=[0123456789]"^
/C:"_Min=[12345][0123456789]" 1>NUL || GoTo GetMins
If %_Min% LEq 9 Set "_Min=0%_Min%"
Echo The 12 hour clock time is %_Hour%:%_Min%%AMPM%
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1>NUL
CodePudding user response:
This is an extended version of the batch file written by Compo.
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Echo ****************** Time Converter ******************
:GetHour
Set "_Hour="
Set /P "_Hour=Please enter the hour> " || GoTo GetHour
Set _Hour | %SystemRoot%\System32\findstr.exe /RXC:"_Hour=[0123456789]" /C:"_Hour=[01][0123456789]" /C:"_Hour=2[0123]" 1>NUL || GoTo GetHour
if %_Hour:~0,1% == 0 if not "%_Hour:~1%" == "" set _Hour=%_Hour:~1%
Set "AMPM=AM" & If %_Hour% GEq 12 If %_Hour% LEq 23 Set "AMPM=PM"
If %_Hour% NEq 12 Set /A _Hour %%= 12
If %_Hour% LEq 9 Set "_Hour=0%_Hour%"
:GetMin
Set "_Min="
Set /P "_Min=Please enter the minute> " || GoTo GetMin
Set _Min | %SystemRoot%\System32\findstr.exe /RXC:"_Min=[0123456789]" /C:"_Min=[012345][0123456789]" 1>NUL || GoTo GetMin
if %_Min:~0,1% == 0 if not "%_Min:~1%" == "" set _Min=%_Min:~1%
If %_Min% LEq 9 Set "_Min=0%_Min%"
Echo The 12 hour clock time is: %_Hour%:%_Min% %AMPM%
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1>NUL
EndLocal
It has following extensions:
- It does not accept
24
for the hour. - It accepts a two digit value for hour and minute with a leading
0
. - It outputs the time always with two digits for the hour and the minute and with a space between minute and
AM
orPM
.