• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 13812
  • Last Modified:

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?
  • 3
  • 2
3 Solutions
%TIME% will give you the 24-hour time

so use

@echo %TIME%
Steve KnightIT ConsultancyCommented:
Try this:


@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%
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


or you could use the sledgehammer approach above!

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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!
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!
Steve KnightIT ConsultancyCommented:
Glad it helped

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now