2026-09-18 · Q&A guide

Extract Day, Month, Year in Windows CMD – Quick Script Guide

Learn how to retrieve the current day, month, and year from a Windows CMD script and store each component in its own variable.

Overview

In batch files you often need the current date split into its parts. Windows exposes the date as the %date% environment variable, but its format depends on the system locale. The goal is to parse that string safely and assign the day, month and year to separate variables that can be used later in the script.

Using the %date% Variable

The %date% variable returns a string such as "Tue 05/23/2024" or "23.05.2024". The exact layout is controlled by the regional settings. Because the format is not fixed, you cannot rely on fixed character positions; instead you should split the string by its delimiters.

Parsing with FOR /F

The FOR /F command can tokenize a string using a set of delimiters. By specifying the delimiters that match the date format (space, slash, dot, comma) you can extract the pieces. The following snippet demonstrates how to split the date into three variables.

for /f "tokens=1-3 delims=/-.":
    %%a in ("%date%") do (
        set "day=%%a"
        set "month=%%b"
        set "year=%%c"
    )

Handling Locale Variations

Because some locales use month/day/year and others use day/month/year, you can detect the order by checking the numeric range of the first token. If the first token is greater than 12, it is the day; otherwise it is the month. Adjust the assignments accordingly.

for /f "tokens=1-3 delims=/-.":
    %%a in ("%date%") do (
        if %%a gtr 12 (
            set "day=%%a"
            set "month=%%b"
            set "year=%%c"
        ) else (
            set "month=%%a"
            set "day=%%b"
            set "year=%%c"
        )
    )

Full Example Script

Below is a complete batch file that captures the date components, normalizes single‑digit values, and echoes them. The script works on any locale because it adapts to the token order.

@echo off
setlocal

:: Split the date string
for /f "tokens=1-3 delims=/-.":
    %%a in ("%date%") do (
        if %%a gtr 12 (
            set "day=%%a"
            set "month=%%b"
            set "year=%%c"
        ) else (
            set "month=%%a"
            set "day=%%b"
            set "year=%%c"
        )
    )

:: Pad single‑digit day/month with a leading zero
if 1!day! lss 20 set "day=0!day!"
if 1!month! lss 20 set "month=0!month!"

:: Output the results
echo Day: !day!
echo Month: !month!
echo Year: !year!
endlocal

Takeaway: You can split the %date% string with FOR /F and delimiters to assign day, month, and year to variables regardless of regional settings.

People also ask

How do I get the date in YYYY-MM-DD format?

After parsing the components, echo them as "%year%-%month%-%day%".

Can I use PowerShell instead of batch?

Yes, PowerShell’s Get-Date cmdlet provides formatted output directly, but the batch method keeps the script native to cmd.exe.

Inspired by a public discussion on Stack Overflow. This article is an original explanation for learners.

← All posts