Batch Script Date and Time
Date and Time
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.
Example
@echo off
echo %date%
07/20/2022
note
The output is different depending on the localization of the operating system.
time
command
The time
command sets or displays the time.
Example
@echo off
echo %time%
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%
Current day : 20
Current month: 07
Current year :2022