Display 24-Hour Time with CMD Command with System Changes

The command: for /f %a in ('time/t') do echo %a

The above command outputs the current local time. What command do I use to get an output in 24-HR time?

For example: If I run this command at 6pm, it will output '6:00pm'. I want to see '18:00' without making any system changes.

Is this possible via a batch script?
deharvyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

darron_chapmanCommented:
%TIME% will give you the 24-hour time

so use

@echo %TIME%
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve KnightIT ConsultancyCommented:
Try this:

Steve

@echo off

for /f "tokens=1,2 delims=:m" %%a in ('time /t') do set hh=%%a & set mm=%%b
REM Get PM or am.  Strip a or p off if it is there.  If 24h already will be ok still
set pm=%mm:~-1,1%
if %pm%=="p" set mm=%mm:~0,-1%
if %pm%=="a" set mm=%mm:~0,-1%

Rem Add leading zeroes to hh
set /a hh=100+%hh%
set hh=%hh:~1%

Rem Add 12 hrs if pm
if "%pm%"=="p" set /a hh=%hh%+12

echo Time is %hh%:%mm%
0
Steve KnightIT ConsultancyCommented:
Haha, good point there... can't remember seeing %time% with am/pm on it so you are probably right there.  It does give hh:mm:ss.ss though so you may want to use

%time:~0,5%

or you could use the sledgehammer approach above!

Steve
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

deharvyAuthor Commented:
Darron was correct when indicating %time%. However, %time:~0,5% was the complete solution. The long script works as well; but for me, the simplier the better. :) Thanks!
0
deharvyAuthor Commented:
echo %time:~0,5% worked just fine for me and this flexible command allows to me many other things like only getting the hour, or minute or second. Thanks for your help!
0
Steve KnightIT ConsultancyCommented:
Glad it helped

Stee
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.