0

I have query to get sysdate in format 25012014. Query is

@echo off
SetLocal enabledelayedexpansion
for /f "tokens=2-4 delims=/ " %%a in ('') do set year=%%c
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set month=%%a
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set day=%%b 
set TODAY1=%day%%month%%year%
echo %TODAY% 

How to get previous days date?

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
user3164140
  • 591
  • 2
  • 8
  • 15
  • possible duplicate of [Batch file: Get yesterday's date in format: M\_d\_yyyy](http://stackoverflow.com/questions/18970662/batch-file-get-yesterdays-date-in-format-m-d-yyyy). There are ***many*** similar questions on StackOverflow. – dbenham Jan 25 '14 at 14:28
  • Using [getTimeStamp.bat](http://www.dostips.com/forum/viewtopic.php?f=3&t=4847), the solution is as simple as `call getTimeStamp -od -1 -f {dd}{mm}{yyyy} -r YESTERDAY` – dbenham Jan 25 '14 at 14:33

2 Answers2

1
:: yesterdays date
@echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "date-yesterday=%mm%%dd%%yyyy%"

echo Yesterday was "%date-yesterday%"
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Hey Thnks, this works for me. Can we have script for T -2 date. i.e. 2 days earlier date. – user3164140 Jan 27 '14 at 03:20
  • You can accept the answer if it worked for you by clicking the tick next to it. Change the 2nd line to `set day=-2` if you want the date 2 days ago. – foxidrive Jan 27 '14 at 05:24
1
@ECHO OFF
SETLOCAL
FOR %%d IN (01 15) DO FOR %%m IN (01 03 05 12) DO FOR /l %%y IN (2010,1,2014) DO (
 SET day=%%d&SET month=%%m&SET year=%%y
 CALL :proc
)

GOTO :EOF

:proc
SET today=%day%%month%%year%
SET /a yesterday=%year% %% 4
SET /a day=1%day%-1
IF %day:~-2%==00 SET /a month=1%month%-1&SET day=131&FOR %%L IN (03 03 05 07 10 12) DO IF %%L==%month% SET /a day-=1
IF %month:~-2%==00 (SET /a month=112&SET /a year-=1) ELSE (IF %yesterday% neq 0 if %month:~-2%==02 set/a day-=1)
SET yesterday=%day:~-2%%month:~-2%%year%
ECHO The day before %today% was %yesterday%
GOTO :eof

The routine :proc actually does the conversion, the triple-nested for loop is just to grab significant dates for testing.

Worked for me!

Magoo
  • 77,302
  • 8
  • 62
  • 84