Retrieve System Time within a Batch File

By | 2015-07-16

I’ve shown you previously how to get the date value from the system in a batch file. You can also grab the time in a similar fashion.

Coupled with the script in the other post, this can be used (for example) to find files that were processed in a certain hour of a particular day. Or, perhaps you could use a bit more processing to say, schedule a task for the current time, one day later. Lots of different possibilities.

Here is the code, by request. As usual, if you have any questions, please feel free to leave it in the comments, or over in the forums.

@echo off

REM ***
REM *** Get Current Time, store in Now. Then use
REM *** string operations to grab Hours, Minutes,
REM *** and seconds, storing each in a variable
REM ***

SET Now=%Time: =0%
SET HH=%Now:~0,2%
SET MM=%Now:~3,2%
SET SS=%Now:~6,2%

REM ***
REM *** Display the output
REM ***

ECHO. Current time = %Now%
ECHO. Hours = %HH%
Echo. Minutes = %MM%
Echo. Seconds = %SS%

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.