How do I get current date/time on the Windows command line in a suitable format for usage in a fi...
Categories:
Mastering Date/Time Formatting for File Names in Windows Command Line

Learn how to obtain and format the current date and time on the Windows command line (CMD) for use in file and folder names, ensuring compatibility and readability.
When working with batch scripts or automating tasks on Windows, it's often necessary to include the current date and/or time in file or folder names. This helps with versioning, logging, and organizing output. However, the default DATE
and TIME
variables in CMD contain characters that are invalid for file paths, such as slashes (/
), colons (:
), and spaces. This article will guide you through various methods to format the date and time into a clean, file-system-friendly string.
Understanding the Challenge: Invalid Characters
The primary challenge stems from the characters used in the default DATE
and TIME
environment variables. Windows file systems (NTFS, FAT32) have restrictions on characters that can be used in file and directory names. Common invalid characters include \ / : * ? " < > |
. The default output of %DATE%
and %TIME%
often contains slashes and colons, making them unsuitable for direct use.
flowchart TD A[Start Batch Script] --> B{Get Current Date/Time} B --> C{Default Format: "DD/MM/YYYY HH:MM:SS.ms"} C --> D{Contains Invalid Characters?} D -- Yes --> E[Need to Format/Replace] D -- No --> F[Use Directly (Rare)] E --> G[Replace Slashes with Hyphens] E --> H[Replace Colons with Hyphens/Dots] E --> I[Remove Spaces/Dots for Milliseconds] G & H & I --> J[Construct Valid Filename String] J --> K[Use in File/Folder Name] K --> L[End Script]
Flowchart illustrating the process of obtaining and formatting date/time for filenames.
Method 1: Using Variable Substring Manipulation
The most common and robust method involves using CMD's built-in variable substring manipulation. This allows you to extract specific parts of the %DATE%
and %TIME%
variables and replace problematic characters. The format of %DATE%
and %TIME%
can vary based on regional settings, but a common approach is to assume a DD/MM/YYYY
and HH:MM:SS.ms
structure.
@echo off
REM Get current date and time
set "currentDate=%date%"
set "currentTime=%time%"
REM Extract and format date parts (assuming DD/MM/YYYY)
set "day=%currentDate:~0,2%"
set "month=%currentDate:~3,2%"
set "year=%currentDate:~6,4%"
REM Extract and format time parts (assuming HH:MM:SS.ms)
set "hour=%currentTime:~0,2%"
set "minute=%currentTime:~3,2%"
set "second=%currentTime:~6,2%"
REM Combine into a file-friendly format (YYYY-MM-DD_HH-MM-SS)
set "timestamp=%year%-%month%-%day%_%hour%-%minute%-%second%"
REM Display the result
echo Formatted Timestamp: %timestamp%
REM Example usage in a filename
echo. > "MyLogFile_%timestamp%.txt"
echo Created MyLogFile_%timestamp%.txt
pause
Batch script using substring manipulation to create a YYYY-MM-DD_HH-MM-SS timestamp.
%DATE%
and %TIME%
formats can change. The wmic
command (Method 3) offers a more locale-independent approach.Method 2: Replacing Invalid Characters Directly
Another approach is to directly replace the invalid characters in the %DATE%
and %TIME%
variables. This can be simpler for quick scripts but might be less precise if you need a very specific output format.
@echo off
REM Get current date and time
set "formattedDate=%date:/=-%"
set "formattedTime=%time::=-%"
REM Remove milliseconds and period from time
set "formattedTime=%formattedTime:~0,8%"
REM Combine into a file-friendly format
set "timestamp=%formattedDate%_%formattedTime%"
REM Display the result
echo Formatted Timestamp: %timestamp%
REM Example usage in a filename
echo. > "AnotherLog_%timestamp%.log"
echo Created AnotherLog_%timestamp%.log
pause
Batch script replacing slashes and colons directly.
Method 3: Using WMIC for Locale-Independent Date/Time
The wmic
(Windows Management Instrumentation Command-line) utility provides a more robust and locale-independent way to get the current date and time. It returns the timestamp in a fixed YYYYMMDDHHMMSS
format, which is ideal for filenames as it contains no invalid characters and sorts chronologically.
@echo off
REM Get timestamp using WMIC
for /f "skip=1" %%x in ('wmic os get LocalDateTime /value') do if not "%%x"=="" set "dt=%%x"
REM Extract the raw timestamp (YYYYMMDDHHMMSS)
set "timestamp=%dt:~17,14%"
REM Optionally, format it with separators for readability
set "year=%timestamp:~0,4%"
set "month=%timestamp:~4,2%"
set "day=%timestamp:~6,2%"
set "hour=%timestamp:~8,2%"
set "minute=%timestamp:~10,2%"
set "second=%timestamp:~12,2%"
set "formattedTimestamp=%year%-%month%-%day%_%hour%-%minute%-%second%"
REM Display the result
echo Raw WMIC Timestamp: %timestamp%
echo Formatted WMIC Timestamp: %formattedTimestamp%
REM Example usage in a filename
echo. > "WMIC_Report_%formattedTimestamp%.csv"
echo Created WMIC_Report_%formattedTimestamp%.csv
pause
Batch script using WMIC to get a locale-independent timestamp.
wmic
command is deprecated in newer versions of Windows (Windows 10 21H1 and later, Windows 11). While it still works, Microsoft recommends using PowerShell for similar tasks. For future-proof scripts, consider PowerShell alternatives.Method 4: PowerShell for Advanced Formatting (and CMD Integration)
For more complex formatting or future-proofing your scripts, PowerShell offers superior date/time manipulation capabilities. You can execute PowerShell commands directly from a CMD batch file.
@echo off
REM Execute PowerShell to get formatted timestamp
for /f "usebackq delims=" %%i in (`powershell -Command "Get-Date -Format 'yyyy-MM-dd_HH-mm-ss'"`) do set "timestamp=%%i"
REM Display the result
echo PowerShell Formatted Timestamp: %timestamp%
REM Example usage in a filename
echo. > "PS_Data_%timestamp%.json"
echo Created PS_Data_%timestamp%.json
pause
Batch script calling PowerShell to get a custom formatted timestamp.
Get-Date -Format
cmdlet provides extensive control over the output format. Refer to the .NET custom date and time format strings documentation for all available options.