Skip to content

How to Reformat a Date String in Batch Script

Learn how to reliably reformat date strings in Windows Batch scripts using substring extraction, wmic, or PowerShell to handle regional settings like MM/DD/YYYY vs DD/MM/YYYY and convert them to ISO format (YYYY-MM-DD).

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
==========================================

Common Scenarios for Date Reformatting

  1. Log File Naming: Converting a date to YYYY-MM-DD ensures that files sorted by name in File Explorer are also sorted chronologically.
  2. Database Import: Many SQL databases require dates in the YYYY-MM-DD format rather than the localized MM/DD/YY.
  3. 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

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

Important Considerations

  1. Substring Offsets: Substring indexing starts at 0.
  2. Length: Always verify if your year is 2 digits (YY) or 4 digits (YYYY).
  3. Delimiters: Some systems use /, some use -, and some use .. The wmic and 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.

Navigation