1.

Solve : How do I Calculate between two date/time strings??

Answer»

I have a program that receives in a string (for the example I put the value in a variable).
This string contains a DATE/TIME that I want a job to run on. I PARSE out the DATE/TIME from the string and compare this to the current date/time but it doesn't want to run (or should I SAY it ALWAYS wants to run even though the date/time is still in the future).

I have put in echo statements that display the results of the strings and everything is correct.
(You can cut and paste this code into a BAT file for testing).

Any help would be appreciated.
Ken



@echo OFF
REM set up the job date and time (hard coded for testing purposes
set JOBFILE="5.200905061159.KWL.J65.Nightly_Process. job"
set RUNDATETIME=%JOBFILE:~3,12%

REM Set up the current time
set CURRENTHOUR=%TIME:~0,2%
if %CURRENTHOUR% LSS 10 set CURRENTHOUR=0%TIME:~1,1%

REM Set up the current date (regardless of format)
REM Set the initial tokens to be 1 through 3 (to be used later)
set $TOK=1-3
REM Take the first part of the string returned by the "date /t" command and put it in the $D1 variable
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $D1=%%u
REM If the first character of the variable $D1 is greater than 9, it must be a letter,
REM therefore the day of WEEK is given, so we adjust our $tok variable to skip the day of week
if "%$D1:~0,1%" GTR "9" set $TOK=2-4
REM Get each part of the date in 3 steps using either parts 1-3 or 2-4 depending on if day of week is given
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
REM get the yy mm and dd from the date [Enter] command to assign the parts in the previous step
for /f "skip=1 tokens=2-4 delims=/-,()." %%X in ('echo.^|date') do (
REM Set the parts of the date
set %%x=%%u
set %%y=%%v
set %%z=%%w
REM clear the $D1 and $TOK variable since we are done with them
set $D1=
set $TOK=
)
)

set CURDATETIME=%yy%%mm%%dd%%CURRENTHOUR%%TIME:~3,2%

echo Calculated current date and time (s/b YYYYMMDDHHMM) %CURDATETIME%
if %RUNDATETIME% GTR %CURDATETIME% goto EOF
echo The sample job (%RUNDATETIME%) would now run
PAUSE

REM ;------------------------------------------------------------------
:EOF
date/time numbers are too long. GTR only works with valid numbers. Batch number variables are limited to 32-bits of precision, as you will see by the error messages if you use set /a to create them.

Since you did not use set /a, the strings are treated as ASCII strings, and since "2" is not greater than "2", the GTR test is failed. Make them shorter. By the way, you don't want any leading zeroes.

Ok great at leas now I can see where the problem is.
How to fix the CURDATETIME to only have YY instead of YYYY.
AND i have no idea how to get rid of the leading zeros with the new calculation.

Help!The reason I said you don't want leading zeroes is because set /a interprets a number starting with a zero as being in octal, so it is a good habit to not use them unless you actually need to.

YY therefore won't do (atleast not until next year)

MMDDhhmm with a leading nonzero digit will work, let's say 1, so that 6th May 19:30 becomes 106051930

Except that there would be a problem with curdatetime being on Dec 31st and rundatetime being on Jan 1st.

But we can use Visual Basic Script to do the comparison:

Code: [Select]@echo OFF

REM Create VBS file

echo Wscript.echo eval(WScript.Arguments(0))>evaluate.vbs

REM set up the job date and time (hard coded for testing purposes
set JOBFILE="5.200905062059.KWL.J65.Nightly_Process. job"
set RUNDATETIME=%JOBFILE:~3,12%

REM Set up the current time
set CURRENTHOUR=%TIME:~0,2%
if %CURRENTHOUR% LSS 10 set CURRENTHOUR=0%TIME:~1,1%

REM Set up the current date (regardless of format)
REM Set the initial tokens to be 1 through 3 (to be used later)
set $TOK=1-3
REM Take the first part of the string returned by the "date /t" command and put it in the $D1 variable
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $D1=%%u
REM If the first character of the variable $D1 is greater than 9, it must be a letter,
REM therefore the day of week is given, so we adjust our $tok variable to skip the day of week
if "%$D1:~0,1%" GTR "9" set $TOK=2-4
REM Get each part of the date in 3 steps using either parts 1-3 or 2-4 depending on if day of week is given
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
REM get the yy mm and dd from the date [Enter] command to assign the parts in the previous step
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
REM Set the parts of the date
set %%x=%%u
set %%y=%%v
set %%z=%%w
REM clear the $D1 and $TOK variable since we are done with them
set $D1=
set $TOK=
)
)

set CURDATETIME=%yy%%mm%%dd%%CURRENTHOUR%%TIME:~3,2%


echo Calculated current date and time (s/b YYYYMMDDHHMM) %CURDATETIME%

echo test: if %RunDateTime% GTR %CurDateTime% don't run job

for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "%rundatetime% > %curdatetime%" ' ) do set Result=%%A

REM Result is -1 if test is passed, 0 if it is failed

if "%Result%"=="-1" goto EOF

echo The sample job would now run

REM ;------------------------------------------------------------------

:EOF

Ok I found a way of doing this in plain old DOS.

I took your advise and added the SET /A.
I also decided to split the date and the time into two test instead of 1 since DOS can't GTR a 12 digit date.

Thanks for your help.
Ken


@ECHO OFF
REM set up the job date and time
set JOBFILE="5.200905060900.KWL.J65.Nightly_Process. job"

REM Extract the job date
set /a RUNDATE=%JOBFILE:~2,8%

REM Set up the current date (regardless of format)
REM Set the initial tokens to be 1 through 3 (to be used later)
set $TOK=1-3
REM Take the first part of the string returned by the "date /t" command and put it in the $D1 variable
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $D1=%%u
REM If the first character of the variable $D1 is greater than 9, it must be a letter,
REM therefore the day of week is given, so we adjust our $tok variable to skip the day of week
if "%$D1:~0,1%" GTR "9" set $TOK=2-4
REM Get each part of the date in 3 steps using either parts 1-3 or 2-4 depending on if day of week is given
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
REM get the yy mm and dd from the date [Enter] command to assign the parts in the previous step
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
REM Set the parts of the date
set %%x=%%u
set %%y=%%v
set %%z=%%w
REM clear the $D1 and $TOK variable since we are done with them
set $D1=
set $TOK=
)
)
set /a CURDATE=%yy%%mm%%dd%

REM Check to see if this job is to be run today, if not skip the rest
if %RUNDATE% LSS %CURDATE% goto NOTRUN

REM Extract the job time
set RUNHOUR=%JOBFILE:~10,2%
if %RUNHOUR% LSS 10 set RUNHOUR=%JOBFILE:~11,1%
set /a RUNTIME=%RUNHOUR%%JOBFILE:~12,2%

REM Set up the current time
set CURRENTHOUR=%TIME:~0,2%
if %CURRENTHOUR% LSS 10 set CURRENTHOUR=0%TIME:~1,1%
set /a CURTIME=%CURRENTHOUR%%TIME:~3,2%

set CURDATETIME=%CURDATE%%CURTIME%
set RUNDATETIME=%RUNDATE%%RUNTIME%

REM Check to see if this job is to run now, if not skip the rest
if %CURTIME% LSS %RUNTIME% goto NOTRUN

echo Job would run %CURDATETIME% (%RUNDATETIME%)
goto EOF

:NOTRUN
echo Job would NOT run %CURDATETIME% (%RUNDATETIME%)

REM ;------------------------------------------------------------------
:EOF
PAUSE
date calc is easier with vbscript
Code: [Select]strJobFile = "5.200905060900.KWL.J65.Nightly_Process.job"
strJobFileYear = Mid(strJobFile,3,4)
strJobFileMth =Mid(strJobFile,7,2)
strJobFileDay =Mid(strJobFile,9,2)
strDate = strJobFileYear & " " & strJobFileMth & " " & strJobFileDay
strNowDate = Year(now) & " " & Month(Now) & " " & Day(Now)
WScript.Echo DateDiff("d",strDate,strNowDate)
Quote from: gh0std0g74 on May 06, 2009, 07:09:33 PM

date calc is easier with vbscript

In every way.



Discussion

No Comment Found