1

I'm writing a batch file to copy some data, the issue I have is, I want to capture the date in a variable so that I can use it as the file name. I would like it in the format of dd-mm-yyyy.

I found this example here, but I was unable to get it into the format I need.

Community
  • 1
  • 1

4 Answers4

2

this solution is independent of locale settings:

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I

It will give you (independent of locale settings!):

  20130802203023.304000+120 
( YYYYMMDDhhmmss.<fraction>+/-<timedifference to UTC>  )

from here, it is easy:

set mydate=%datetime:~6,2%-%datetime:~4,2%-%datetime:~0,4%
echo %mydate%
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

Solution

set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set newdate=%dd%%mm%%yy%
echo %newdate%
0
:date
set dateyear=%date:~-4%
if %date:~-7,-5%==01 set datemonth=January
if %date:~-7,-5%==02 set datemonth=February
if %date:~-7,-5%==03 set datemonth=march
if %date:~-7,-5%==04 set datemonth=April
if %date:~-7,-5%==05 set datemonth=may
if %date:~-7,-5%==06 set datemonth=June
if %date:~-7,-5%==07 set datemonth=July
if %date:~-7,-5%==08 set datemonth=August
if %date:~-7,-5%==09 set datemonth=September
if %date:~-7,-5%==10 set datemonth=October
if %date:~-7,-5%==11 set datemonth=November
if %date:~-7,-5%==12 set datemonth=December
set dateday=%date:~-10,-8%
echo It is %dateday% %datemonth% %dateyear%, sir.
exit /B 0
:xpdate
echo It is %date%, sir.
%speech% "It is %date%, sir."
exit /B 0
:w7dat
if %time:~0,-9% GTR 12 (set /a timehour=%time:~0,-9%-12) else set timehour=%time:~0,-9%
if %time:~3,1%==0 (set timeminute=%time:~4,1%) else set timeminute=%time:~3,2%
set dateyear=%date:~-4%
if %date:~-7,-5%==01 set datemonth=January
if %date:~-7,-5%==02 set datemonth=February
if %date:~-7,-5%==03 set datemonth=march
if %date:~-7,-5%==04 set datemonth=April
if %date:~-7,-5%==05 set datemonth=may
if %date:~-7,-5%==06 set datemonth=June
if %date:~-7,-5%==07 set datemonth=July
if %date:~-7,-5%==08 set datemonth=August
if %date:~-7,-5%==09 set datemonth=September
if %date:~-7,-5%==10 set datemonth=October
if %date:~-7,-5%==11 set datemonth=November
if %date:~-7,-5%==12 set datemonth=December
set dateday=%date:~-10,-8%
echo It is currently %timeminute% minutes past %timehour% on %dateday% %datemonth% %dateyear%, sir.
exit /B 0

This is what i use in my batch AI just change the :date :w7dat and all those to what you need

0

Here is a solution using PowerShell, which is pre-installed on nearly all versions of Windows and is far more powerful than batch.

PowerShell code

Get-Date -Format 'yyyy-MM-dd'

Calling from a batch file

for /f %%a in ('powershell -nop -c "Get-Date -Format 'yyyy-MM-dd'"') do set newdate=%%a
Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54