how to print previous date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

how to print previous date

#1 Post by santhosh » 04 Aug 2010 08:28

hi.
help me out...
how to print previous date in a variable
eg:
today is 04/08/2010
output:
030810

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: how to print previous date

#2 Post by !k » 04 Aug 2010 08:56

Code: Select all

@echo off
echo WScript.Echo DateAdd("D", WScript.Arguments(0), Now)> "%temp%\dt+.vbs"

call :dayadd -1
echo [today-1] %$d:/=%

call :dayadd +1
echo [today+1] %$d:/=%

del /q "%temp%\dt+.vbs"
pause
goto :eof

:dayadd
for /f "tokens=1*" %%i In ('cscript //Nologo "%temp%\dt+.vbs" %~1') do set "$d=%%i"
goto :eof

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: how to print previous date

#3 Post by santhosh » 07 Aug 2010 08:25

HI thanks man,

but is they anyother way to do it without using wscript

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to print previous date

#4 Post by aGerman » 07 Aug 2010 08:55

This is complicated. Batch can't calculate with DateTime values directly. You could calculate "julian days" from your date, subtract 1 and calculate it back.
http://www.dostips.com/DtTipsDateTime.php#Function.date2jdate
http://www.dostips.com/DtTipsDateTime.php#Function.jdate2date

Regards
aGerman

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: how to print previous date

#5 Post by santhosh » 08 Aug 2010 11:43

ohh ok thnks agerman

then only this way is possible i have to store a date in a text file and copy that next day. :cry:

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to print previous date

#6 Post by aGerman » 08 Aug 2010 12:08

Didn't you try the functions I linked?

Code: Select all

@echo off &setlocal

set Ytoday=2010
set Mtoday=08
set Dtoday=08

:: calculate the julian days
call :date2jdate jd %Ytoday% %Mtoday% %Dtoday%

:: subtract 1
set /a jd-=1

:: calculate the new values
call :jdate2date %jd% Yyesterday Myesterday Dyesterday

:: combine the returned values
echo %Dyesterday%%Myesterday%%Yyesterday:~-2%

pause
goto :eof


:date2jdate JD YYYY MM DD -- converts a gregorian calender date to julian day format
::                        -- JD   [out] - julian days
::                        -- YYYY [in]  - gregorian year, i.e. 2006
::                        -- MM   [in]  - gregorian month, i.e. 12 for december
::                        -- DD   [in]  - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set "yy=%~2"&set "mm=%~3"&set "dd=%~4"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b

:jdate2date JD YYYY MM DD -- converts julian days to gregorian date format
::                     -- JD   [in]  - julian days
::                     -- YYYY [out] - gregorian year, i.e. 2006
::                     -- MM   [out] - gregorian month, i.e. 12 for december
::                     -- DD   [out] - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set /a L= %~1+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
set /a YYYY= I,  MM=100+J,  DD=100+K
set MM=%MM:~-2%
set DD=%DD:~-2%
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
    IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
    IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%
)
EXIT /b


You could calculate %Ytoday%, %Mtoday% and %Dtoday% from %date%. But this depends on the format that echo %date% would return. It's DD.MM.YYYY for my settings, but I don't know which format it is for your settings.

Regards
aGerman

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: how to print previous date

#7 Post by santhosh » 08 Aug 2010 12:55

thanks agerman,

i seen that link .......i wrote most of my scripts in shell. but this is first time in dos .......just to print previous date i have to write that much big script. :( so i didnt gone through that julian matter.

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: how to print previous date

#8 Post by santhosh » 09 Aug 2010 13:13

hi agerman,

when i ran this script yesterday it displayed previous date(070810) ..ok its fine.........

but when i ran today it dispalyed same date(070810) :(

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to print previous date

#9 Post by aGerman » 09 Aug 2010 13:40

Of course. The date is "hard coded":
set Ytoday=2010
set Mtoday=08
set Dtoday=08

Please give me the date format of your settings.
Open a command prompt as follows:
press [Windows]+[R]
write cmd
click OK

Now write echo %date%
and hit [Enter]

Post me the output, then I can make it working dynamic.

Regards
aGerman

miskox
Posts: 631
Joined: 28 Jun 2010 03:46

Re: how to print previous date

#10 Post by miskox » 09 Aug 2010 13:59

This might help.

This tip is here (newdate.bat):

http://www.fpschultze.de/modules/PDdown ... d=1&lid=35

The original part is this:

Code: Select all

REM echo   MM-DD-YYYY  DD-MM-YYYY  DD.MM.YYYY  DD/MM/YYYY
  :Begin --------------------------------------------------------------
   echo. | date | FIND "(mm" > NUL
   if NOT errorlevel 1 %0 : %OS%Parse MM DD
                       %0 : %OS%Parse DD MM
  :Windows_NTParse ----------------------------------------------------
   for /F "tokens=1-4 delims=/.- " %%A in ('date /T') do if %%D!==! (
     set %3=%%A&set %4=%%B&set YYYY=%%C
   ) else (
     set DOW=%%A&set %3=%%B&set %4=%%C&set YYYY=%%D)
   goto End


As yo can see this part of the batch script is capable of handling all types of date format.

Question remains (should be checked with localized version of Windows) if it can handle non-English versions ("(mm" should then be replaced).

Saso

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: how to print previous date

#11 Post by santhosh » 09 Aug 2010 15:15

hi agerman,

its below one
Tue 08/10/2010

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to print previous date

#12 Post by aGerman » 09 Aug 2010 15:25

Try

Code: Select all

@echo off &setlocal

:: calculate the julian days
for /f "tokens=2-4 delims=/ " %%a in ("%date%") do call :date2jdate jd %%c %%a %%b

:: subtract 1
set /a jd-=1

:: calculate the new values
call :jdate2date %jd% Yyesterday Myesterday Dyesterday

:: combine the returned values
echo %Dyesterday%%Myesterday%%Yyesterday:~-2%

pause
goto :eof


:date2jdate JD YYYY MM DD -- converts a gregorian calender date to julian day format
::                        -- JD   [out] - julian days
::                        -- YYYY [in]  - gregorian year, i.e. 2006
::                        -- MM   [in]  - gregorian month, i.e. 12 for december
::                        -- DD   [in]  - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set "yy=%~2"&set "mm=%~3"&set "dd=%~4"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b

:jdate2date JD YYYY MM DD -- converts julian days to gregorian date format
::                     -- JD   [in]  - julian days
::                     -- YYYY [out] - gregorian year, i.e. 2006
::                     -- MM   [out] - gregorian month, i.e. 12 for december
::                     -- DD   [out] - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set /a L= %~1+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
set /a YYYY= I,  MM=100+J,  DD=100+K
set MM=%MM:~-2%
set DD=%DD:~-2%
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
    IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
    IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%
)
EXIT /b


Regards
aGerman

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: how to print previous date

#13 Post by santhosh » 09 Aug 2010 15:45

yes thanks agerman

its working

kindly solve that problem which i posted in delete column forum.......its very urgent.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: how to print previous date

#14 Post by avery_larry » 10 Aug 2010 11:40

aGerman --

not sure about languages nor OS versions, but here's a trick for (XP English)regional settings:

Code: Select all

for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
    for /f "tokens=1-3 delims=/.- " %%A in ("%Date:* =%") do (
        set %%a=%%A&set %%b=%%B&set %%c=%%C)
    )
)

set /a "yy=10000%yy% %% 10000 %% 2000 + 2000,mm=100%mm% %% 100,dd=100%dd% %% 100"
From there you can calculate the julian dates using yy mm dd

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: how to print previous date

#15 Post by jeb » 10 Aug 2010 11:47

It only works on an english version, because you get something like

"current date is 10/08/2010"
"Enter new date: (DD-MM-YY)"

or it also works with
"Enter new date: (YY-DD-MM)"

but in german you got
"Geben Sie das neue Datum ein: (TT-MM-JJ)"
So the script can't find %yy% nor %dd%, and I can't see a way how to decide which chars are for the year or day,
perhaps in some countries you get
"?#+#++ : (RR-ZZ-XX)"

@!k what is the response in your version?

Post Reply