How do I get current date/time on the Windows command line in a suitable format for usage in a fi...
Categories:
Getting Current Date/Time on Windows Command Line for Filenames

Learn how to format the current date and time on the Windows command line (CMD) into a suitable string for use in file and folder names, avoiding invalid characters and ensuring proper sorting.
When working with batch scripts or the command line on Windows, a common requirement is to include the current date and time in file or folder names. This is crucial for logging, backups, or creating unique identifiers. However, directly using the output of standard date/time commands often results in characters (like /, :, ) that are invalid in Windows filenames. This article will guide you through various methods to obtain a clean, usable date/time string.
Understanding the Challenge: Invalid Filename Characters
Windows file systems have specific rules for characters allowed in filenames. The following characters are generally forbidden: \ / : * ? " < > |. Standard commands like DATE and TIME or environment variables like %DATE% and %TIME% often include these forbidden characters. For example, %DATE% might output 10/26/2023 and %TIME% might output 14:35:01.23. Directly concatenating these would lead to invalid filenames.
echo %DATE%
echo %TIME%
Shows typical output of the %DATE% and %TIME% environment variables.
Method 1: Using Variable Substring Manipulation
The most common and robust method involves using variable substring manipulation to extract specific parts of the %DATE% and %TIME% variables and then concatenating them in a desired format. This method is highly flexible and works across different Windows versions.
@echo off
REM Get current date and time
set "YYYYMMDD=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%"
set "HHMMSS=%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%"
REM Combine for a unique filename string
set "DATETIME_STR=%YYYYMMDD%_%HHMMSS%"
echo Formatted Date/Time: %DATETIME_STR%
REM Example usage in a filename
echo This is a test > "log_%DATETIME_STR%.txt"
pause
A batch script to create a YYYYMMDD_HHMMSS formatted string.
%DATE:~10,4%) might vary slightly depending on your Windows region settings for date format. For instance, if your date format is MM/DD/YYYY, the year might be at a different index. Test echo %DATE% to determine the correct indices for your system.Method 2: Using WMIC (Windows Management Instrumentation Command-line)
WMIC can also be used to get the current date and time in a structured format, which can then be parsed. While often more verbose, it provides a consistent output regardless of regional settings, making it a good option for more robust scripts.
@echo off
for /f "skip=1 tokens=1-6" %%a in ('wmic path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /format:list') do (
if "%%a" neq "" set %%a
)
set "YYYY=%Year%"
set "MM=0%Month%"
set "DD=0%Day%"
set "HH=0%Hour%"
set "MI=0%Minute%"
set "SS=0%Second%"
REM Pad with leading zeros if necessary
set "MM=%MM:~-2%"
set "DD=%DD:~-2%"
set "HH=%HH:~-2%"
set "MI=%MI:~-2%"
set "SS=%SS:~-2%"
set "DATETIME_WMIC=%YYYY%%MM%%DD%_%HH%%MI%%SS%"
echo Formatted Date/Time (WMIC): %DATETIME_WMIC%
pause
Using WMIC to get and format the date and time.

Process flow for date/time formatting on Windows command line.
Choosing the Right Format and Best Practices
For filenames, a YYYYMMDD_HHMMSS format (e.g., 20231026_143501) is generally recommended because:
- Lexicographical Sorting: Files sorted alphabetically will also be sorted chronologically.
- Uniqueness: Provides a high degree of uniqueness, especially when including seconds.
- Readability: Easy to understand the date and time at a glance.
Always enclose filenames with variables in double quotes ("") to handle potential spaces or special characters in other parts of the filename, although our formatted date/time string avoids them.