Output the Date in a Windows Batch Script

This simple script will output the date in the MMDDYYYY format. The script can be used to enhance other scripts, for log files, etc.

@echo off
@REM Setups %date variable
@REM First parses month, day, and year into mm , dd, yyyy formats and then combines to be MMDDYYYY
@REM The date variable will be in the MMDDYYYY format so it can be called later in the script

FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%

@echo on
echo hello %date%
pause

One comment

  1. If you need the day (Sun, Mon, Tue, Wed, Thu, Fri, Sat):

    for /f “TOKENS=1” %%a in (‘DATE/T’) do set DAY=%%a
    echo %DAY%
    pause

Leave a Reply