Skip to main content

How to Get the Current Date/Time into Variables in Batch Script

Capturing the current date and time is one of the most common requirements for any automation script. You need it to create timestamped log entries, generate unique filenames for backups, or record when a specific action occurred. Windows Batch provides two built-in, dynamic variables, %DATE% and %TIME%, for this purpose.

However, a major challenge with these variables is that their format changes based on the user's regional and language settings. This guide will teach you how to reliably parse these variables to extract their components, how to build a standardized and sortable timestamp, and introduce the more robust WMIC method for scripts that need to be internationally compatible.

The Core Variables: %DATE% and %TIME%

The command processor provides two special variables that are always available:

  • %DATE%: Expands to the current date.
  • %TIME%: Expands to the current time.
@ECHO OFF
ECHO The current date is: %DATE%
ECHO The current time is: %TIME%

Output (in a typical US-English locale)

The current date is: Fri 10/27/2023
The current time is: 10:30:15.75
note

The leading space in the time if the hour is a single digit (e.g., 9:30...).

The Problem: Locale-Dependent Formats

The biggest weakness of %DATE% and %TIME% is that their output format is not fixed. It changes based on the Windows "Short date" and "Short time" settings.

  • US: Fri 10/27/2023
  • UK: 27/10/2023
  • Germany: 27.10.2023
  • ISO 8601: 2023-10-27

A script that tries to parse the US format will break on a UK system. This makes the variables inherently unreliable for scripts that need to be shared or run in different environments.

The Standard Solution: Parsing with Substring Extraction

If you know your script will only ever run on systems with a consistent regional setting (e.g., only on US-English systems), you can reliably parse the string using substring extraction.

An example of script for US MM/DD/YYYY format

@ECHO OFF
ECHO The full date is: %DATE%

REM --- Deconstruct the %DATE% variable ---
SET "YYYY=%DATE:~10,4%"
SET "MM=%DATE:~4,2%"
SET "DD=%DATE:~7,2%"

ECHO.
ECHO --- Parsed Components ---
ECHO Year: %YYYY%
ECHO Month: %MM%
ECHO Day: %DD%
warning

This method is fast and simple but not portable!

Creating a Robust, Sortable Timestamp (YYYY-MM-DD)

The goal of parsing is often to create a standardized, sortable timestamp, like 2023-10-27. This format is internationally understood (ISO 8601) and allows files to be sorted chronologically by name.

Example of Script for US MM/DD/YYYY format:

@ECHO OFF
SET "YYYY=%DATE:~10,4%"
SET "MM=%DATE:~4,2%"
SET "DD=%DATE:~7,2%"

SET "HH=%TIME:~0,2%"
SET "MIN=%TIME:~3,2%"
SET "SEC=%TIME:~6,2%"

REM Handle the leading space in the time for hours < 10
IF "%HH:~0,1%"==" " SET "HH=0%HH:~1,1%"

SET "SortableDateTime=%YYYY%-%MM%-%DD%_%HH%-%MIN%-%SEC%"

ECHO The sortable timestamp is: %SortableDateTime%

Output:

The sortable timestamp is: 2023-10-27_10-30-15

A More Reliable Method for Portability: WMIC

For any script that needs to be robust and run reliably across different machines, the WMIC (Windows Management Instrumentation) command is the superior and recommended method. It can provide the date and time in a fixed, locale-independent format.

Command: WMIC OS GET LocalDateTime

This command returns the date and time in a single, unformatted string that is always the same: YYYYMMDDHHMMSS.fractional+timezone.

Script to Parse WMIC Output

@ECHO OFF
ECHO Getting a locale-independent timestamp with WMIC...

FOR /F "tokens=2 delims==" %%I IN ('WMIC OS GET LocalDateTime /VALUE') DO SET "dt=%%I"
REM The above line gets a value like 20231027103015.750000-240

SET "YYYY=%dt:~0,4%"
SET "MM=%dt:~4,2%"
SET "DD=%dt:~6,2%"
SET "HH=%dt:~8,2%"
SET "MIN=%dt:~10,2%"
SET "SEC=%dt:~12,2%"

ECHO.
ECHO Year: %YYYY%, Month: %MM%, Day: %DD%
ECHO Hour: %HH%, Minute: %MIN%, Second: %SEC%
note

This method is immune to the user's regional settings and is the professional way to get the date and time in a script.

Common Pitfalls and How to Solve Them

  • Locale-Dependency: This is the biggest pitfall. A script that parses %DATE% will fail in a different country. Solution: Use the WMIC method for any script that will be shared.
  • Invalid Filename Characters: The default %TIME% contains colons (:), and %DATE% can contain slashes (/), neither of which are allowed in filenames. Solution: You must parse the components and reassemble them, or use string substitution to remove the invalid characters (e.g., SET "SafeTime=%TIME::=%" ).
  • Leading Space in Time: The %TIME% variable has a leading space for hours before 10 AM (e.g., " 9:05"). Solution: Handle this with an IF statement or with a replacement: SET "HH=%TIME:~0,2%" & SET "HH=%HH: =0%".

Practical Example: Creating a Timestamped Backup Filename

This script uses the robust WMIC method to create a uniquely named backup file.

@ECHO OFF
SETLOCAL
SET "BACKUP_DIR=E:\Backups"
MKDIR "%BACKUP_DIR%" 2>NUL

ECHO --- Daily Backup Script ---
ECHO.

REM --- Get timestamp using the reliable WMIC method ---
FOR /F "tokens=2 delims==" %%I IN ('WMIC OS GET LocalDateTime /VALUE') DO SET "dt=%%I"
SET "TimeStamp=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%"

SET "BACKUP_FILENAME=ProjectBackup_%TimeStamp%.zip"
SET "FULL_PATH=%BACKUP_DIR%\%BACKUP_FILENAME%"

ECHO Creating backup file: "%FULL_PATH%"

REM (Backup command would go here)
ECHO "This is a backup" > "%FULL_PATH%"

ECHO.
ECHO [SUCCESS] Backup created.

ENDLOCAL

Conclusion

Getting the current date and time is essential for dynamic scripting, but the standard %DATE% and %TIME% variables are unreliable due to their dependency on regional settings.

  • Parsing with substring extraction is acceptable for simple scripts that you know will only ever be run in a single, known locale.
  • The WMIC OS GET LocalDateTime method is the overwhelmingly superior and recommended best practice. It provides a fixed, locale-independent format that is perfect for writing robust, portable, and professional scripts.