Page 1 of 1
Is there an easy way to echo 31 days from current date?
Posted: 12 Sep 2011 07:12
by Ken
Hello All...
Is there an easy way to echo to the screen the date showing 31 days ahead of the current date each time the batch file is executed?
example: - The file is opened today and the system date is 9/12/2011 - I would like it to echo to screen 10/13/2011.
Tomorrows system date 9/13/2011 - echoed to screen 10/14/2011 etc...
Thank you everyone for all your help
Ken
Re: Is there an easy way to echo 31 days from current date?
Posted: 12 Sep 2011 09:33
by !k
batch + vbs
Code: Select all
@echo off
echo WScript.Echo DateAdd("D", WScript.Arguments(0), Now)> "%temp%\dt+.vbs"
call :dayadd +31
echo [today+31] %$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: Is there an easy way to echo 31 days from current date?
Posted: 12 Sep 2011 10:12
by Ken
Thank you for the quick response...
Is there any way to do this without creating and using a vbs file?
I know the temp file gets deleted after the file is closed but can this be done without any files being created using dos batch.
Thank you
Ken
Re: Is there an easy way to echo 31 days from current date?
Posted: 12 Sep 2011 11:14
by trebor68
I have used the two function ":date2jdate" and ":jdate2date" from this forum (see homepage from this forum).
Please change the date format to your format. The code will work correctly in Germany.
Here a solution:
Code: Select all
@echo off
echo.
echo See the day that we have in 31 days.
echo.
:: German : dd.mm.yyyy
:: English: mm\dd\yy please change to: set monat=%help:~0,2% -- set tag=%help:~3,2% -- set jahr=%help:~6,2%
set help=%date%
set tag=%help:~0,2%
set monat=%help:~3,2%
set jahr=%help:~6,4%
:: set /p jahr=Please input the year :
:: set /p monat=Please input the month:
:: set /p tag=Please input the day :
call :date2jdate help %jahr% %monat% %tag%
set /a help+=31
call :jdate2date %help% jahr monat tag
echo.
echo The date is: %monat%/%tag%/%jahr%
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
Re: Is there an easy way to echo 31 days from current date?
Posted: 12 Sep 2011 12:08
by Ken
Thank You Robert and !k...
I saw the code from this site... I was just hoping there was something with maybe a few lines of code. I will use what is posted here.
Thank you again
Ken
Re: Is there an easy way to echo 31 days from current date?
Posted: 12 Sep 2011 16:58
by nitt
Ken wrote:Thank You Robert and !k...
I saw the code from this site... I was just hoping there was something with maybe a few lines of code. I will use what is posted here.
Thank you again
Ken
Well, think about it. 31 days doesn't necessarily mean the same day next month. There's a lot more math to be involved.
Re: Is there an easy way to echo 31 days from current date?
Posted: 13 Sep 2011 08:04
by trebor68
Here a code a little bit smaler.
Code: Select all
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
echo.
echo See the day that we have in 31 days.
echo.
:: German : dd.mm.yyyy
:: English: mm\dd\yyyy please change to: set monat=%help:~0,2% -- set tag=%help:~3,2% -- set jahr=%help:~6,4%
set help=%date%
(set tag=%help:~0,2%) & (set monat=%help:~3,2%) & set jahr=%help:~6,4%
set /a "j=10000%jahr% %%10000,m=100%monat% %% 100,t=100%tag% %% 100"
(set /a help=j-j/4*4,m+=1) & if !help! equ 0 (set sj=1) else set sj=0
if %monat% equ 01 if %tag% gtr 28 (
set /a m=3,t=tag-28-sj
if %tag%%sj%==291 (set t=29)&(set m=2)
)
if %monat% equ 02 set /a t=tag+3-sj
if %tag% equ 31 for %%a in (03 05 08 10) do if %monat% equ %%a set /a m+=1,t=1
for %%a in (04 06 09 11) do if %monat% equ %%a set /a t+=1
if %m% equ 13 set /a j+=1,m=1
echo.
echo The day is (dd.mm.yyyy): %t%.%m%.%j%