System Date -90 Days and -180 Days

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: System Date -90 Days and -180 Days

#16 Post by aGerman » 13 Jul 2013 15:10

I understood your problem well :wink:
I'll implement the registry method into Antonios code

Code: Select all

@echo off
setlocal
call :DateToJDN "%date%" jdn=
set /A minus90=jdn-90, minus180=jdn-180
call :JDNtoDate %minus90% minus90=
call :JDNtoDate %minus180% minus180=
echo Today minus 90 days: %minus90%, minus 180 days: %minus180%
pause
goto :EOF


:DateToJDN locale_date_format jdn=
setlocal

set "date=%~1"
for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"^|findstr /i "\<[is]Date\>"') do set "%%a=%%c"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%date:* =%") do (
  if %iDate%==0 (set /a mm=100%%a%%100,dd=100%%b%%100,yyyy=10000%%c%%10000) else (
  if %iDate%==1 (set /a dd=100%%a%%100,mm=100%%b%%100,yyyy=10000%%c%%10000) else (
  if %iDate%==2 (set /a yyyy=10000%%a%%10000,mm=100%%b%%100,dd=100%%c%%100)
)))
if %yyyy% LSS 100 if %yyyy% LSS 70 (set /a yyyy=2000+yyyy) else (set yyyy=1900+yyyy)

set /A a=(mm-14)/12, jdn=(1461*(yyyy+4800+a))/4+(367*(mm-2-12*a))/12-(3*((yyyy+4900+a)/100))/4+dd-32075
endlocal & set %2=%jdn%
exit /B


:JDNtoDate jdn yyyymmdd=
setlocal
set /A l=%1+68569,n=(4*l)/146097,l=l-(146097*n+3)/4,i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447
set /A dd=l-(2447*j)/80,l=j/11,mm=j+2-(12*l),yyyy=100*(n-49)+i+l
if %dd% lss 10 set dd=0%dd%
if %mm% lss 10 set mm=0%mm%
endlocal & set %2=%yyyy%%mm%%dd%
exit /B

Regards
aGerman

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: System Date -90 Days and -180 Days

#17 Post by Ocalabob » 13 Jul 2013 20:21

@aGerman
I hard coded the date in your script changing

Code: Select all

call :DateToJDN %date% jdn=
to
call :DateToJDN 07/13/2013 jdn=

Screenshot

Code: Select all

C:\!DOSTIPS>aGerman2.bat
Today minus 90 days: 20130414, minus 180 days: 20130114
Press any key to continue . . .

The math is working I'll figure out the %date% part later.

Thank you!

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: System Date -90 Days and -180 Days

#18 Post by Ocalabob » 13 Jul 2013 20:33

@penpen
Your script gives the date in the correct format which I could probably use with aGermans script.
Screenshot

Code: Select all

SET_DATE_FORMAT = "MM-DD-YY"
DATE_FORMAT     = "DDDD MM/DD/YYYY"
07/13/2013
Press any key to continue . . .


Thank you!

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: System Date -90 Days and -180 Days

#19 Post by Ocalabob » 13 Jul 2013 21:14

@foxidrive
I did additional editing of your script for testing and I also wanted to leave some 'bread crumbs' for my future editing.
Good work. Thank you!
Results:

Code: Select all

C:\!DOSTIPS>date_math.bat
Todays date is                      07132013
Todays date with seperator is  07_13_2013
90 days ago the date was "04-14-2013" 180 days ago the date was "01-14-2013"

Tomorrow's date is "07*14*2013"
30 days from today is "08/12/2013"
30 days from today in yyyy/mm/dd format is 2013/08/12
Press any key to continue . . .

Edited Scripts

Code: Select all

::date_math.bat
@echo off
Setlocal

call date+-.bat today 0
set today=%day%

call date+-.bat today 0 _
set today2=%day%

call date+-.bat today -90 -
set today90=%day%

call date+-.bat today -180 -
set today180=%day%

call date+-.bat today 1 *
set nextday=%day%

call date+-.bat today 30 /
set day30=%day%

echo.

echo Todays date is                "%today%"
echo Todays date with seperator is "%today2%
echo 90 days ago the date was "%today90%" 180 days ago the date was "%today180%"
echo.
echo Tomorrow's date is "%nextday%"
echo 30 days from today is "%day30%"
echo 30 days from today in yyyy/mm/dd format is %day2%
pause
Exit

Code: Select all

::This file is to be called by date_math.bat
:: Date+-.bat

@echo off
if "%~2"=="" (
echo to get todays date use         call "%~n0" today 0
echo to get yesterdays date use     call "%~n0" today -1
echo to get 25 days before 19441213 call "%~n0" 1944/12/13 -25
echo to get 1250 days in the future call "%~n0" today 1250
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: to use - as in YYYY-MM-DD for today's date
echo     call "%~n0" today 0 -
echo.
pause
goto :EOF)

set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"

::Line below gives the date in the format yyyy/mm/dd
endlocal& set day2=%result:~0,4%%separator%%result:~4,2%%separator%%result:~6,2%

::Line below gives the date in the format mm/dd/yyyy
endlocal& set day=%result:~4,2%%separator%%result:~6,2%%separator%%result:~0,4%

::echo %%day%% is set to "%day%" (without the quotes)
::pause


edited typos

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

Re: System Date -90 Days and -180 Days

#20 Post by aGerman » 14 Jul 2013 04:39

Seems I didn't read your replies carefully enough. The script would also support a date format with spaces like Fri 07/12/2013. You have to enclose the variable into quotation marks though.

Code: Select all

call :DateToJDN "%date%" jdn=

Also changed it in the code above.

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: System Date -90 Days and -180 Days

#21 Post by foxidrive » 14 Jul 2013 05:01

Hi Ocalabob.

I should say that the code isn't mine - I fiddled the help and small modifications but Phil Robyn posted the VBS script many years ago.
At the time it seemed such a good/simple function which is independent from all local settings that I keep re-posting it.

A way to configure the date format would be useful but given yyyymmdd then that is easy to change in batch anyway.

(later)
Still, you gave me the impetus to modify it a little in the help and the last 6 lines, so that it sets YY MM DD variables also, and people can use them individually.
As it's often used in folder names and filenames, YYYYMMDD sorts properly in a computer directory so in these cases it is a better format than other variations.


:: Date foward & backward

Code: Select all

@echo off
if "%~2"=="" (
echo to get todays date use         call "%~n0" today 0
echo to get yesterdays date use     call "%~n0" today -1
echo to get 25 days before 19441213 call "%~n0" 1944/12/13 -25
echo to get 1250 days in the future call "%~n0" today 1250
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: for this format YYYY-MM-DD using today's date
echo     call "%~n0" today 0 -
echo.
pause
goto :EOF)

set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~0,4%"&set "MM=%result:~4,2%"&set "DD=%result:~6,2%"
set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
echo %%YY%% is set to %YY%
echo %%MM%% is set to %MM%
echo %%DD%% is set to %DD%

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: System Date -90 Days and -180 Days

#22 Post by Ocalabob » 14 Jul 2013 18:57

@aGerman, the quotations solve the date problem for me. Thank you!
@foxidrive, those are welcome modifications. I test the script and it works great. Thank you!

Comment:
Now all we need is a question to go with these solutions! :)

Post Reply