Page 1 of 2
how to print previous date
Posted: 04 Aug 2010 08:28
by santhosh
hi.
help me out...
how to print previous date in a variable
eg:
today is 04/08/2010
output:
030810
Re: how to print previous date
Posted: 04 Aug 2010 08:56
by !k
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
Re: how to print previous date
Posted: 07 Aug 2010 08:25
by santhosh
HI thanks man,
but is they anyother way to do it without using wscript
Re: how to print previous date
Posted: 07 Aug 2010 08:55
by aGerman
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.date2jdatehttp://www.dostips.com/DtTipsDateTime.php#Function.jdate2dateRegards
aGerman
Re: how to print previous date
Posted: 08 Aug 2010 11:43
by santhosh
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.
Re: how to print previous date
Posted: 08 Aug 2010 12:08
by aGerman
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
Re: how to print previous date
Posted: 08 Aug 2010 12:55
by santhosh
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.
Re: how to print previous date
Posted: 09 Aug 2010 13:13
by santhosh
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)
Re: how to print previous date
Posted: 09 Aug 2010 13:40
by aGerman
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 cmdclick OKNow write echo %date%and hit [Enter]Post me the output, then I can make it working dynamic.
Regards
aGerman
Re: how to print previous date
Posted: 09 Aug 2010 13:59
by miskox
This might help.
This tip is here (newdate.bat):
http://www.fpschultze.de/modules/PDdown ... d=1&lid=35The 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
Re: how to print previous date
Posted: 09 Aug 2010 15:15
by santhosh
hi agerman,
its below one
Tue 08/10/2010
Re: how to print previous date
Posted: 09 Aug 2010 15:25
by aGerman
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
Re: how to print previous date
Posted: 09 Aug 2010 15:45
by santhosh
yes thanks agerman
its working
kindly solve that problem which i posted in delete column forum.......its very urgent.
Re: how to print previous date
Posted: 10 Aug 2010 11:40
by avery_larry
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
Re: how to print previous date
Posted: 10 Aug 2010 11:47
by jeb
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?