43

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

alroc
  • 27,574
  • 6
  • 51
  • 97
Jacek
  • 11,661
  • 23
  • 69
  • 123

6 Answers6

94

This will give you DD MM YYYY YY HH Min Sec variables and works on any Windows machine from XP Pro and later.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
phuclv
  • 37,963
  • 15
  • 156
  • 475
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • 4
    I'd never expect to find such a useful tool here: [WMIC](https://technet.microsoft.com/en-us/library/bb742610.aspx#EDAA) -- thanks for the introduction :-) – Wolf Dec 01 '15 at 13:44
  • When running manually on the commandline I had to remove the extra % from %%a : for /f "tokens=2 delims==" %a in ('wmic OS Get localdatetime /value') do set "dt=%a" (but when putting it in a script you need the extra %) – AmanicA Oct 22 '20 at 11:17
  • This is fantastic. Thanks! – Kip Feb 18 '21 at 17:10
41

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.

Kip
  • 107,154
  • 87
  • 232
  • 265
Ozan Yurtseven
  • 810
  • 8
  • 17
  • 23
    This is dependent on the regional settings on the computer. If it works then good, but it is not a robust method. – foxidrive Oct 02 '13 at 07:07
  • 2
    You should better add that your solution works with an English locale. Especially in my case date is the following `01.12.2015`, so this solution may have helped (which is great), but its value is limited. Make the limitation explicit, and I guess you'll possibly get some more upvotes. – Wolf Dec 01 '15 at 13:24
  • 1
    @Wolf there are many different English locales with different date formats like AU, UK, US... – phuclv Mar 18 '16 at 08:27
  • @LưuVĩnhPhúc Yes, that's true. My point was to explicitly add its limitation to the solution shown :) – Wolf Mar 18 '16 at 09:39
29

%date% will give you the date.

%time% will give you the time.

The date and time /t commands may give you more detail.

canhazbits
  • 1,664
  • 1
  • 14
  • 19
10

Locale-independent one liner to get any date format you like. I use it to generate archive names. Back quote (`) option is needed because PowerShell command line is using single quotes (').

:: Get date in 'yyyyMMdd_HHmm' format to use with file name.
FOR /f "usebackq" %%i IN (`PowerShell ^(Get-Date^).ToString^('yyyyMMdd_HHmm'^)`) DO SET DTime=%%i

:: Get yesterday date in 'yyyy-MM-dd' format.
FOR /f "usebackq" %%i IN (`PowerShell ^(Get-Date^).AddDays^(-1^).ToString^('yyyy-MM-dd'^)`) DO SET DTime=%%i

:: Show file name with the date.
echo Archive.%DTime%.zip
Evaldas Jocys
  • 169
  • 1
  • 3
6
set datestr=%date%
set result=%datestr:/=-%
@echo %result%
pause
jfindley
  • 682
  • 7
  • 6
0

This will give you the date in this format mmddyyyy saved into %today%, assuming your regional settings have you in the US locale.

FOR /f "tokens=2-4 delims=/ " %%a IN ("%date%") DO SET today=%%a%%b%%c

In my case I was looking to create a folder with todays date, like this...

mkdir C:\Users\LKrell\Documents\%today%

Luke Krell
  • 13
  • 4