Search code examples
windowsbatch-file

How to get date in BAT file


I need to get today date in Window *.bat file. After it I would like to get day, month and year. How can I do this?
I can't use PowerShell


Solution

  • You get and format like this

    for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
         set dow=%%i
         set month=%%j
         set day=%%k
         set year=%%l
    )
    set datestr=%month%_%day%_%year%
    echo datestr is %datestr%
    

    Note: Above only works on US locale. It assumes the output of echo %date% looks like this: Thu 02/13/21. If you have different Windows locale settings, you will need to modify the script based on your configuration.