System Date -90 Days and -180 Days

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
vsr1258
Posts: 5
Joined: 06 Mar 2013 05:39

System Date -90 Days and -180 Days

#1 Post by vsr1258 » 03 Jul 2013 23:35

Mod edit: This poster removed his question

The question was posed to provide a method to get dates 90 days and 180 days in the past, from today, and use them in a command line like this:

Code: Select all

Start D:\folder\Utilities\XYZExtractCmdLine_x64.exe -d "C:\folder" -u C:\folder -a ABC -l ; -s (System Date/Current Date – 180 Days) -e (System Date/Current Date – 90 Days) -k(enabled) –r(enabled)
Last edited by vsr1258 on 11 Jul 2013 03:25, edited 2 times in total.

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

Re: System Date & system Date - 90 Days Batch Script needed

#2 Post by foxidrive » 04 Jul 2013 04:15

Save the batch file at the end and call it date+-.bat then try this.

Code: Select all

@echo off
echo "Your program is running"

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

echo "%today90%" "%today180%"
pause

Start D:\folder\Utilities\XYZExtractCmdLine_x64.exe -d "C:\folder" -u C:\folder -a ABC -l ; -s %today180% -e %today90 -k(enabled) –r(enabled)

Exit




Code: Select all

:: 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"
endlocal& set day=%result:~0,4%%separator%%result:~4,2%%separator%%result:~6,2%
echo %%day%% is set to "%day%" (without the quotes)
pause

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

Re: System Date & system Date - Days Batch Script needed

#3 Post by Ocalabob » 10 Jul 2013 19:25

@foxidrive
I tested your scripts; modified the echo lines for my use. Results are below.

Code: Select all

C:\>main_one
"Your program is running"
%day% is set to "20130710" (without the quotes)
Press any key to continue . . .
%day% is set to "20130411" (without the quotes)
Press any key to continue . . .
%day% is set to "20130111" (without the quotes)
Press any key to continue . . .

Todays date is "20130710"
90 days ago the date was "20130411" 180 days ago the date was "20130111"
Press any key to continue . . .

I think I'll tuck those scripts away for future use.

vsr1258
Posts: 5
Joined: 06 Mar 2013 05:39

Re: System Date & system Date - Days Batch Script needed

#4 Post by vsr1258 » 11 Jul 2013 03:27

Yes it's worked.

Once again thanks a lot!!!!!!

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

Re: System Date & system Date - Days Batch Script needed

#5 Post by foxidrive » 11 Jul 2013 08:10

Ocalabob wrote:@foxidrive
I tested your scripts; modified the echo lines for my use. Results are below.

Todays date is "20130710"
90 days ago the date was "20130411" 180 days ago the date was "20130111"

I think I'll tuck those scripts away for future use.


Bewdy! I like them because it's fairly straightforward, not like rolling your own in batch

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: System Date -90 Days and -180 Days

#6 Post by Aacini » 11 Jul 2013 22:30

Below there is a pure Batch solution that use Date-Julian Day Number conversions to get dates in past or future by a direct subtract/add of any number of days to the Julian Day Number.

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%
goto :EOF


:DateToJDN mm/dd/yyyy jdn=
setlocal
for /F "tokens=1-3 delims=/" %%a in ("%1") do (
   set /A mm=1%%a-100, dd=1%%b-100, yyyy=%%c
)
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

This solution assume that ECHO %DATE% show the date in MM/DD/YYYY format and that the past date is required in YYYYMMDD format, but these formats may be easily modified if needed.

Antonio

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

Re: System Date -90 Days and -180 Days

#7 Post by foxidrive » 12 Jul 2013 04:00

Aacini wrote:This solution assume that ECHO %DATE% show the date in MM/DD/YYYY format and that the past date is required in YYYYMMDD format, but these formats may be easily modified if needed.

Antonio


Antonio, it would be perfect if the system date format could be predictable - it's blindingly stupid that Windows doesn't provide a simple environment variable of a predictable format to use with date and time calculations and manipulations. It's been a FAQ ever since MSdos days!

That's why I often post VBS and WMIC solutions with robust solutions for any locale.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: System Date -90 Days and -180 Days

#8 Post by Aacini » 12 Jul 2013 10:51

@foxidrive,

Yes, this is a problem if you want to create a general use, locale independant solution. However, many users of Batch files just want to solve a specific problem in their own computers, and this type of solution is adequate for most of them...

Antonio

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

Re: System Date -90 Days and -180 Days

#9 Post by Ocalabob » 12 Jul 2013 19:17

@Antonio
I am testing your script this evening and ran into a problem. Below are my results: (2nd)

Code: Select all

C:\>echo %date%
Fri 07/12/2013

C:\>date_it.bat
Invalid number.  Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
Today minus 90 days: -47130826, minus 180 days: -47130528
C:\>

Thoughts?

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: System Date -90 Days and -180 Days

#10 Post by Aacini » 12 Jul 2013 22:14

@Ocalabob,

Yes. ECHO %DATE% is assumed to show the date in MM/DD/YYYY format, but in your case the first token is the day of week. Change this line:

Code: Select all

for /F "tokens=1-3 delims=/" %%a in ("%1") do (

by this one:

Code: Select all

for /F "tokens=2-4 delims=/ " %%a in ("%1") do (


Antonio

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

Re: System Date -90 Days and -180 Days

#11 Post by foxidrive » 13 Jul 2013 03:48

That's precisely why it is a poor solution in the internet days. Other people will come googling in the future, find the code and it doesn't work.

I tried it too, without giving it any forethought, and also had the trouble that Ocalabob had.

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

Re: System Date -90 Days and -180 Days

#12 Post by Ocalabob » 13 Jul 2013 11:49

Antonio,
I made the adjustment and that solved the error problem. Thank you.
I still have a date format problem.

Screenshot

Code: Select all

C:\>date_it.bat
Today minus 90 days: -47130826, minus 180 days: -47130528
C:\>

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

Re: System Date -90 Days and -180 Days

#13 Post by aGerman » 13 Jul 2013 14:09

I still have a date format problem.

Sometimes it's not that simple :wink: I don't remember where we already discussed that theme. Basically i see two solutions.
1) Read the format informations from the registry.
2) Use WMIC (only for the current date).

Code: Select all

@echo off &setlocal

echo Read registry values
echo iDate (order 0=MDY, 1=DMY, 2=YMD) and sDate (Separator)
for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"^|findstr /i "\<[is]Date\>"') do set "%%a=%%c"
echo Found    order: '%iDate%'    separator: '%sDate%'

echo(
echo Current date: %date%

echo(
echo Strings
for /f "tokens=1-3 delims=%sDate%" %%a in ("%date:* =%") do (
  if %iDate%==0 (set mm=%%a&set dd=%%b&set yy=%%c) else (
  if %iDate%==1 (set dd=%%a&set mm=%%b&set yy=%%c) else (
  if %iDate%==2 (set yy=%%a&set mm=%%b&set dd=%%c)
)))
REM Workaround for 2 digit year
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
echo %yy% %mm% %dd%

echo(
echo Numbers
for /f "tokens=1-3 delims=%sDate%" %%a in ("%date:* =%") do (
  if %iDate%==0 (set /a m=100%%a%%100,d=100%%b%%100,y=10000%%c%%10000) else (
  if %iDate%==1 (set /a d=100%%a%%100,m=100%%b%%100,y=10000%%c%%10000) else (
  if %iDate%==2 (set /a y=10000%%a%%10000,m=100%%b%%100,d=100%%c%%100)
)))
REM Workaround for 2 digit year
if %y% LSS 100 if %y% LSS 70 (set /a y=2000+y) else (set y=1900+y)
echo %y% %m% %d%

echo(
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo WMIC makes life easier
for /f "skip=1 delims=." %%d in ('WMIC OS Get LocalDateTime^|findstr .') do set "timestamp=%%d"
echo %timestamp%

echo(
pause

Regards
aGerman

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: System Date -90 Days and -180 Days

#14 Post by penpen » 13 Jul 2013 14:41

There is also a not short but simple batch only code that may resolve it:

Code: Select all

@echo off
cls
setlocal

call ::initDateFormats
call :parseDateStringToMMDDYYYY MM DD YYYY

echo SET_DATE_FORMAT = "%SET_DATE_FORMAT%"
echo DATE_FORMAT     = "%DATE_FORMAT%"
echo %MM%/%DD%/%YYYY%


endlocal
goto:eof


:initDateFormats
::   
   setlocal enableDelayedExpansion

::   save current date
   set "CURRENT_DATE=%DATE%"

::   build SET_DATE_FORMAT losing actual pc date
   (set "NOT="  & for /f "tokens=*" %%a in ('date 90-90-90') do set "NOT=%%a" )<nul
   (set "YY_1=" & for /f "tokens=*" %%a in ('date 90-01-01') do set "YY_1=%%a")<nul
   (set "YY_2=" & for /f "tokens=*" %%a in ('date 01-90-01') do set "YY_2=%%a")<nul
   (set "DD_1=" & for /f "tokens=*" %%a in ('date 20-01-01') do set "DD_1=%%a")<nul
   (set "DD_2=" & for /f "tokens=*" %%a in ('date 01-20-01') do set "DD_2=%%a")<nul

          if not "%YY_1%" == "%NOT%" (if "%DD_2%" == "%NOT%" ( set "SET_DATE_FORMAT=YY-MM-DD" ) else ( set "SET_DATE_FORMAT=YY-DD-MM" )
   ) else if not "%YY_2%" == "%NOT%" (if "%DD_1%" == "%NOT%" ( set "SET_DATE_FORMAT=MM-YY-DD" ) else ( set "SET_DATE_FORMAT=DD-YY-MM" )
   ) else                            (if "%DD_1%" == "%NOT%" ( set "SET_DATE_FORMAT=MM-DD-YY" ) else ( set "SET_DATE_FORMAT=DD-MM-YY" )
   )

::   build DATE_FORMAT losing actual date
   call ::setDateMMDDYY 02 01 03
   call :dateTokenize TOKEN DATE
   set "DATE_FORMAT=%DATE%" & for %%a in (%TOKEN%) do (
             if "%%a" == "01"   ( set "DATE_FORMAT=!DATE_FORMAT:01=DD!"
      ) else if "%%a" == "1"    ( set "DATE_FORMAT=!DATE_FORMAT:1=D!"
      ) else if "%%a" == "02"   ( set "DATE_FORMAT=!DATE_FORMAT:02=MM!"
      ) else if "%%a" == "2"    ( set "DATE_FORMAT=!DATE_FORMAT:2=M!"
      ) else if "%%a" == "2003" ( set "DATE_FORMAT=!DATE_FORMAT:2003=YYYY!"
      ) else if "%%a" == "03"   ( set "DATE_FORMAT=!DATE_FORMAT:03=YY!"
      ) else if "%%a" == "3"    ( set "DATE_FORMAT=!DATE_FORMAT:3=Y!"
      ) else                    ( set "DATE_FORMAT=!DATE_FORMAT:%%a=DDDD!"
      )
      set /a "INDEX+=1"
   )

::   restore current date
   call :setDateString CURRENT_DATE

::   save DATE_FORMAT and SET_DATE_FORMAT
   endlocal & set "DATE_FORMAT=%DATE_FORMAT%" & set "SET_DATE_FORMAT=%SET_DATE_FORMAT%"
   goto:eof



:dateTokenize
::   %1 variable returning the tokens
::   %2 variable containing date string representation
   setlocal enableDelayedExpansion
   set "TOKEN=!%2!"
   set "TOKEN=%TOKEN:,= %"
   set "TOKEN=%TOKEN:-= %"
   set "TOKEN=%TOKEN:.= %"
   set "TOKEN=%TOKEN:/= %"
   set "TOKEN=%TOKEN::= %"
   set "TOKEN=%TOKEN:;= %"
   set "TOKEN=%TOKEN:\= %"
   endlocal & set "%1=%TOKEN%"
   goto :eof

:setDateMMDDYY
::   SET_DATE_FORMAT must exist and be valid
::   %1 day to set
::   %2 month to set
::   %3 year to set
   setlocal enableDelayedExpansion
   set "DATE_STRING=%SET_DATE_FORMAT%"
   set "DATE_STRING=!DATE_STRING:MM=%1!"
   set "DATE_STRING=!DATE_STRING:DD=%2!"
   set "DATE_STRING=!DATE_STRING:YY=%3!"
   date %DATE_STRING%
   endlocal
   goto :eof


:setDateString
::   DATE_FORMAT and SET_DATE_FORMAT must exist and be valid
::   %1 variable storing the   date string to set
   setlocal enableDelayedExpansion

   call :parseDateStringToMMDDYYYY MM DD YYYY %1
   call :setDateMMDDYY %MM% %DD% %YYYY:~2,2%

   endlocal
   goto :eof


:parseDateStringToMMDDYYYY
::   DATE_FORMAT and SET_DATE_FORMAT must exist and be valid
::   %1 variable name for returning the month
::   %2 variable name for returning the day
::   %3 variable name for returning the year
::   %4 variable name containing the Date
   setlocal enableDelayedExpansion
   set "DATE_STRING=%4"
   if not defined DATE_STRING set "DATE_STRING=DATE"

::   storing the indices of day, month and year in D, M, and Y
   call :dateTokenize TOKEN DATE_FORMAT
   set "Y=" & set "M=" & set "D=" & set "INDEX=0" & for %%a in (%TOKEN%) do (
             if "%%a" == "DD"   ( set "D=!INDEX!"
      ) else if "%%a" == "D"    ( set "D=!INDEX!"
      ) else if "%%a" == "MM"   ( set "M=!INDEX!"
      ) else if "%%a" == "M"    ( set "M=!INDEX!"
      ) else if "%%a" == "YYYY" ( set "Y=!INDEX!"
      ) else if "%%a" == "YY"   ( set "Y=!INDEX!"
      ) else if "%%a" == "Y"    ( set "Y=!INDEX!"
      )
      set /a "INDEX+=1"
   )

::   storing the values of day, month and year in D, M, and Y
   call :dateTokenize TOKEN %DATE_STRING%
   set "INDEX=0" & for %%a in (%TOKEN%) do (
             if "!INDEX!" == "%D%" ( SET "D=%%a"
      ) else if "!INDEX!" == "%M%" ( SET "M=%%a"
      ) else if "!INDEX!" == "%Y%" ( SET "Y=%%a"
      )
      set /a "INDEX+=1"
   )

   if "%D:~1,1%" == "" set "D=0%D%"
   if "%M:~1,1%" == "" set "M=0%M%"
   if "%Y:~1,1%" == "" set "Y=200%Y%"
:: 1930 to 2029 is the default setting, maybe changed: then you have to change this, too.
   if %Y% LSS 30  set "Y=20%Y%"
   if %Y% LSS 100 set "Y=19%Y%"

   endlocal & set "%1=%M%" & set "%2=%D%" & set "%3=%Y%"
   goto:eof

This was the solution (call ::initDateFormats) that should be used only on autoexec.bat within dos, so you can use the functions below.
When running Windows it is not recommended as even on boot time setting the date to multiple values can have various side effects.
For this you can set the two format strings manually for a user, so you can make use of the other (not resetting dates) functions.

penpen

Edit: If no side effects are assumed this code also works on windows cmd.exe, and you can copy the date format strings from the shell.

Edit2:You should take care of side effects (harmless word, but critical results):
For example if a virus scanner runs only for 4 years, or so, and you run this
batch it may be noticed by the virus scanner and confusing it, as
this batch sets multiple dates, and after that restoring the current date.

This result may be, that the virus scanner believes a couple of years have passed, and the 4 year period just ended.

Because of such things is it not recommended, to run this initializiation under windows 95+ (only recommended for MS-DOS 6.xx, 7.0x, 71x)
Last edited by penpen on 13 Jul 2013 15:34, edited 1 time in total.

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

Re: System Date -90 Days and -180 Days

#15 Post by Ocalabob » 13 Jul 2013 14:50

@aGerman
For clarification, when I was testing Antonio's script I was looking for results in the format of:
Today minus 90 days: 04-14-2013, minus 180 days: 01-14-2013

Instead, my results were:
Today minus 90 days: -47130826, minus 180 days: -47130528

Thanks for your post. I'm off to go test it now.

Oops, just noticed penpen's post so I'll give it a test as well. :)

Post Reply