Skip to content

Batch Script Date and Time

Learn how to retrieve, display, and extract system date and time in Windows Batch Scripting using date, time, and WMIC commands for locale-independent formatting.

The date and time in Batch Scripting have the following two basic commands for retrieving the date and time of the system.

date command

The date command gets the system date.

Examplebatch
@echo off 
echo %date%
Outputplaintext
07/20/2022

time command

The time command sets or displays the time.

Examplebatch
@echo off 
echo %time%
Outputplaintext
18:15:44.79

Extract current day/month/year in a locale-independent way

The following batch code returns the components of the current date in a locale-independent way and stores day, month and year in the variables CurrDay, CurrMonth and CurrYear, respectively:

@echo off 
for /F "skip=1 delims=" %%F in ('
    wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE
') do (
    for /F "tokens=1-3" %%L in ("%%F") do (
        set CurrDay=0%%L
        set CurrMonth=0%%M
        set CurrYear=%%N
    )
)
set CurrDay=%CurrDay:~-2%
set CurrMonth=%CurrMonth:~-2%
echo Current day  :  %CurrDay%
echo Current month:  %CurrMonth%
echo Current year :%CurrYear%
Outputplaintext
Current day  :  20
Current month:  07
Current year :2022
Navigation