How to Reformat a Date String in Batch Script
Handling dates is one of the most frustrating tasks in Batch because the default %DATE% variable format changes depending on the user's Regional Settings (e.g., MM/DD/YYYY in the US vs DD/MM/YYYY in Europe). To build a reliable script that reformats a date, you must "Slice" the string into individual parts (Year, Month, Day) and then reassemble them in your desired order.
In this guide, we will demonstrate how to rearrange a date string using variable substrings.
The Strategy: The Sliced String
Most Windows systems provide a date in the format Wday MM/DD/YYYY or DD/MM/YYYY. We will use the substring syntax ~start,length to extract the pieces.
Implementation Script
@echo off
setlocal
:: 1. Capture the current date
:: Assume format is: Mon 12/25/2023
set "origDate=%DATE%"
echo Original Date: %origDate%
:: 2. Identify the Positions (Varies by Region!)
:: For MM/DD/YYYY starting at index 4 (skipping 'Mon '):
:: M is 4,5 | D is 7,8 | Y is 10,11,12,13
set "mm=%origDate:~4,2%"
set "dd=%origDate:~7,2%"
set "yy=%origDate:~10,4%"
echo.
echo Extracted:
echo Month: %mm%
echo Day: %dd%
echo Year: %yy%
:: 3. Reformat to ISO (YYYY-MM-DD) for sorting
set "isoDate=%yy%-%mm%-%dd%"
echo.
echo ==========================================
echo NEW FORMAT (ISO): %isoDate%
echo ==========================================
pause
endlocal
Output:
Original Date: Sun 04/12/2026
Extracted:
Month: 04
Day: 12
Year: 2026
==========================================
NEW FORMAT (ISO): 2026-04-12
==========================================
This script assumes %DATE% outputs Wday MM/DD/YYYY (e.g., Mon 12/25/2023). The format varies by Windows region and language. Hardcoded substring offsets will extract wrong values on machines with a different locale. For portable scripts, use the wmic or PowerShell methods shown below.
Common Scenarios for Date Reformatting
- Log File Naming: Converting a date to
YYYY-MM-DDensures that files sorted by name in File Explorer are also sorted chronologically. - Database Import: Many SQL databases require dates in the
YYYY-MM-DDformat rather than the localizedMM/DD/YY. - Cross-Region Scripts: If you share a script with a colleague in a different country, reformatting ensures their computer doesn't crash because it expected a day at position 4 but found a month instead.
The wmic Method (Locale-Independent)
Since %DATE% is unreliable across different computers, a more portable way to get the date parts is to query wmic. This returns a fixed YYYYMMDD... format regardless of regional settings.
Robust Universal Date Script
@echo off
setlocal
:: Get datetime from WMIC (independent of locale)
for /f "tokens=2 delims==" %%I in (
'wmic os get localdatetime /value'
) do for /f "delims=" %%J in ("%%I") do set "dt=%%J"
:: Extract parts
set "yy=%dt:~0,4%"
set "mm=%dt:~4,2%"
set "dd=%dt:~6,2%"
echo Universal Format (YYYY-MM-DD): %yy%-%mm%-%dd%
pause
endlocal
wmic is deprecated starting in Windows 10 21H1 and may be removed in future Windows releases. For long-lived scripts, prefer the PowerShell method shown in the next section.
PowerShell Method (Modern Alternative)
For scripts that must run reliably on current and future Windows versions, call PowerShell to retrieve a pre-formatted date string:
@echo off
setlocal
for /f "usebackq delims=" %%D in (
`powershell -NoProfile -Command "Get-Date -Format 'yyyy-MM-dd'"`
) do set "isoDate=%%D"
echo Universal Format (YYYY-MM-DD): %isoDate%
pause
endlocal
Get-Date -Format lets you specify any custom pattern. For example, 'yyyyMMdd_HHmmss' produces a timestamp like 20231225_143005 that is ideal for log-file naming.
Important Considerations
- Substring Offsets: Substring indexing starts at
0. - Length: Always verify if your year is 2 digits (
YY) or 4 digits (YYYY). - Delimiters: Some systems use
/, some use-, and some use.. Thewmicand PowerShell methods are preferred because they avoid these differences entirely.
Conclusion
Reformatting date strings is a vital "Sanitization" step for any script that handles file naming or database logging. By mastering string slicing and utilizing robust locale-independent methods, you ensure your automation behaves consistently across different localized versions of Windows. This level of environmental awareness is what separates basic hobbyist scripts from professional, portable system administration tools.