[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Calculate Date. Date - Date2 = 2 days different

Posted on 2009-02-16
21
Medium Priority
?
482 Views
Last Modified: 2012-05-06
I have a program which i have to run over 2 - 4 days. Its a selection process which selects customers who ordered within a set date. Now im scripting this process as it takes up a week of my time to run the process. However i need all the selections to start on the same date. In the selection fields i have to select t-1095 which is todays date minus 1095 days. however if i run the process the next day i need it to be t-1096 taking into account that another day has passed.

Now i need a way to create a start selection date, so lets say today. 16/02/2009 when i run script 1 today it gives me t-1095 however if i run another script tomorrow it gives me t-1096 and if a wait 10 days it gives me t-1105. I basically need a formula to allow me to set the start date and then give me the difference in days. IE today-startday

I have tried to look at other scripts however my knowledge of data manipulation is limited. I need to be able to do this in a batch file.
0
Comment
Question by:purpleoak
  • 9
  • 7
  • 4
  • +1
21 Comments
 
LVL 11

Expert Comment

by:willettmeister
ID: 23651739
In reality a date to a computer is x time from a certain date know of the epoch.  If you set a variable for your start day in terms of time since the epoch you should be able to easily calcuate the current days date based on the time from the epoch then script you actions that occur based on the epoch date.  
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23652658
Why is t-1095 important? Why can't the script just say the start date is 17/02/2006?
0
 
LVL 39

Expert Comment

by:BillDL
ID: 23653144
Perhaps a coincidence, but 1,095 days = exactly 3 years based on 365 days a year.  It sounds like it's a lookup for something like when 3 year interest free credit runs out, or when a 3 year warranty cover expires.  Get ready with your Leap Year code, Amazingtech ;-)
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 21

Expert Comment

by:AmazingTech
ID: 23660644
Yeah. I'm ready to put it up but there's going to be a need to some kind of starting point.

So in the batch file let's say we set.

16/02/2009 - 1095 days

But since this is static anyways why not just type in 17/02/2009?

Unless of course there are multiple days and you don't want the person to calculate the end date.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23660654
But since this is static anyways why not just type in 17/02/2006?
0
 
LVL 39

Expert Comment

by:BillDL
ID: 23664138
purpleoak, are you there to explain the details and help AmazingTech write you a solution?
0
 
LVL 2

Author Comment

by:purpleoak
ID: 23683056
Sorry for the delay.

We are working with a program call PRIAM, terrible program. One of the process is to run a search called RFV. which selects customers who have an account created between x date and y date. then ordered between Z date and y date. Y date would be todays date. The system has two ways of selecting the date. T which means today, or 19feb09 DDMMMYY its a strange program. We have to run 8 of these searchs however we can only run one per day. I need to script this selection. See screenshot of the RFV selection.

I would like to define a start date, EG Today. The searchs are t-182, t-365, t-730 etc. If i run search 1 today t-182 that will give me the correct date, however if i run the same search tomorrow it will be a day out, i need to be able to counter for this time discrepancy.

If this makes sence, i need to be able to set a start date, 182, 365, 730 are always the same, then when i run the script if its run on the start date then 182 remains the same if its 3 days past 182 becomes 185 and 365 becomes 368 etc. I need to be able to calculate how many days have passed since i set the start date. then ammend the numbers 182, 365 accordingly.
RFV.bmp
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23684096
Can we not always say in a batch file?

02/19/2009 - 182
02/19/2009 - 365
02/19/2009 - 730

So that when you run it tomorrow or 10 days from now it will still give you the same date?

What is your date format you use on your computer?

From the command line:
echo %date%
0
 
LVL 2

Author Comment

by:purpleoak
ID: 23690189
my date format is 20/02/2009 DD/MM/YYYY that fine i can set the date via the command line, however i would need to convert the month to a three letter word. ie 01=JAN, 02=FEB, 03=MAR etc, i have a rough idea how this can be done, but i don't know how to dissect the date format.
0
 
LVL 39

Accepted Solution

by:
BillDL earned 2000 total points
ID: 23691184
You can do it in several ways:

Parse the output of the command and break it into tokens defined by the / delimeter.  %%a will be the first (dd), %%b will be the 2nd (mm), and %%c will be the 3rd (yyyy).

@echo off

for /f "tokens=1,2,3 delims=/" %%a in ('echo %date%') do echo DD = %%a MM = %%b YYYY = %%c

echo.
pause
exit

You can set the %%a, %%b, and %%c variables to other more suitably named ones that can be recalled at any time further on in the batch file if you choose:

@echo off

for /f "tokens=1,2,3 delims=/" %%a in ('echo %date%') do (

    set DD=%%a
    set MM=%%b
    set YYYY=%%c
)

echo Day = %DD%      Month = %MM%      Year = %YYYY%

echo.
pause
exit

Another versatile method is using modifiers on the output of the %date% command.  The 1st number is the starting point of the string counted from the leftmost character which is zero, and the number after the comma is how many characters to grab from the string.  The idea is to miss out the / characters, and you an add other characters into the screen output at the ECHO command:

@echo off

rem My Date format is 20/02/2009
rem Looking to get (2009)(02)(20) ie. (yyyy)(mm)(dd)

set NEWDATE=(%DATE:~6,4%)(%DATE:~3,2%)(%DATE:~0,2%)

echo Modified (yyyy)(mm)(dd) Date using new variable is: %NEWDATE%
echo.
echo Modified (yyyy)(mm)(dd) Date just echoed is: (%DATE:~6,4%)(%DATE:~3,2%)(%DATE:~0,2%)
echo.

pause
set NEWDATE=
exit

I hope this allows you to see how each method works, and enable you to manipulate the theory for your own use.
0
 
LVL 39

Assisted Solution

by:BillDL
BillDL earned 2000 total points
ID: 23691231
This clunky way will allow you to set the 3-character Month from the %DATE% output:

@echo off

if "%DATE:~3,2%" EQU "01" set MONTH=JAN
if "%DATE:~3,2%" EQU "02" set MONTH=FEB
if "%DATE:~3,2%" EQU "03" set MONTH=MAR
if "%DATE:~3,2%" EQU "04" set MONTH=APR
if "%DATE:~3,2%" EQU "05" set MONTH=MAY
if "%DATE:~3,2%" EQU "06" set MONTH=JUN
if "%DATE:~3,2%" EQU "07" set MONTH=JUL
if "%DATE:~3,2%" EQU "08" set MONTH=AUG
if "%DATE:~3,2%" EQU "09" set MONTH=SEP
if "%DATE:~3,2%" EQU "10" set MONTH=OCT
if "%DATE:~3,2%" EQU "11" set MONTH=NOV
if "%DATE:~3,2%" EQU "12" set MONTH=DEC

echo The current Month is: %MONTH%

echo.
pause
set MONTH=
exit

I didn't add any alternative if the IF test results in something other than a 01 to 12, but it shouldn't.
0
 
LVL 39

Expert Comment

by:BillDL
ID: 23691252
....
You can obviously echo the results as a full date, like this if you wanted:

echo Today's date is: %DATE:~0,2%-%MONTH%-%DATE:~6,4%
0
 
LVL 39

Expert Comment

by:BillDL
ID: 23691275
Waiting for AmazingTech to work his magic ;-)
I just answered the direct question while AmazingTech was sleeping.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23691997
OK. I just woke up and getting ready for work. I'll post when I get to work.
0
 
LVL 2

Author Comment

by:purpleoak
ID: 23692557
for /f "tokens=1,2,3 delims=/" %%a in ('echo %date1%') do (

    set DD=%%a
    set MM=%%b
    set YYYY=%%c
)
if "%DATE1:~3,2%" EQU "01" set MONTH=JAN
if "%DATE1:~3,2%" EQU "02" set MONTH=FEB
if "%DATE1:~3,2%" EQU "03" set MONTH=MAR
if "%DATE1:~3,2%" EQU "04" set MONTH=APR
if "%DATE1:~3,2%" EQU "05" set MONTH=MAY
if "%DATE1:~3,2%" EQU "06" set MONTH=JUN
if "%DATE1:~3,2%" EQU "07" set MONTH=JUL
if "%DATE1:~3,2%" EQU "08" set MONTH=AUG
if "%DATE1:~3,2%" EQU "09" set MONTH=SEP
if "%DATE1:~3,2%" EQU "10" set MONTH=OCT
if "%DATE1:~3,2%" EQU "11" set MONTH=NOV
if "%DATE1:~3,2%" EQU "12" set MONTH=DEC

echo %DD%%month%%YYYY%



this gives me exactly what i need i managed to work the rest of it out, thank you very much for your help everyone.
0
 
LVL 2

Author Closing Comment

by:purpleoak
ID: 31549253
Thank you very much for your help
0
 
LVL 39

Expert Comment

by:BillDL
ID: 23693832
Thank you purpleoak, I'm glad you found something here that helped you achieve the end result.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23694425
Great going BillDL.

Here's what I came up with.

Currently will only work if your current system date format is numeric. Day of the week is ignored if you use it.

Need some more explaination just ask.

I didn't want to let BillDL down.  I think he was waiting for this.
@Set DateOutputFormat=
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
Set DaysInMonth=31,28,31,30,31,30,31,31,30,31,30,31
Set monthnames=JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC
CALL :GetCurrentDateFormat
REM **************************************************************************************************
REM ** Posted on EE by AmazingTech
REM ** You can put your code under here                                                             **
REM ** Current date format must have 4 digit year                                                   **
REM ** All functions must be called with the current date format (day of the week will be ignored)  **
REM **************************************************************************************************
 
REM When DateOutputFormat is not set it will be set to CurrentDateFormat
CALL :Function_DATEADD %Date% -1000
REM The part after Function_ is the new variable for the output formatted as defined in DateOutputFormat
ECHO %DATEADD%
REM You can also use the variables d,m and y.
ECHO Day=%d%
ECHO Month=%m%
ECHO Year=%y%
 
Set DateOutputFormat=ddmmmyy
CALL :Function_DATEADD 01/01/2009 -182
ECHO %DATEADD%
 
 
CALL :Function_DATEADD 01/01/2009 -365
ECHO %DATEADD%
 
 
CALL :Function_DATEADD 01/01/2009 -1095
ECHO %DATEADD%
 
Set DateOutputFormat=yyyy.mmm.dd
CALL :Function_DATEADD %DATE% +374
ECHO %DATEADD%
 
Set DateOutputFormat=mmm.dd.yyyy
CALL :Function_DATEADD 01/01/2008 -1201
ECHO %DATEADD%
 
CALL :Function_GetOutputAsCurrentFormat
ECHO %GetOutputAsCurrentFormat%
 
Set DateOutputFormat=ddmmmyy
CALL :Function_DATEADD %GetOutputAsCurrentFormat% -120
ECHO %DATEADD%
 
CALL :Function_GetOutputAsCurrentFormat
CALL :Function_DATEADD %GetOutputAsCurrentFormat% +121
ECHO %DATEADD%
 
 
REM ********************************************************************************************************
REM ** Everything below here are my functions                                                             **
REM ** I know it's long and I'll be trying to reduce the number of lines shortly                          **
REM **                                                                                                    **
REM ** Functions to use in your code above:                                                               **
REM **                                                                                                    **
REM ** Adds or subtracts days - Function_DATEADD {date in current system date format} {+/-days no spaces} **
REM ** Changes the output to current system date format - Function_GetOutputAsCurrentFormat               **
REM ********************************************************************************************************
 
GOTO :EOF
 
:Function_DATEADD
Set WorkingDate=%1
if "%WorkingDate:~9,1%" == "" SHIFT
Set WorkingDate=%1
Set WorkingDate=%WorkingDate:~-10%
Set WorkingDate=!WorkingDate:%sDate%=/!
for /f "tokens=1,2,3 delims=/" %%a in ('echo !CurrentDateFormat!') do for /f "tokens=1,2,3 delims=/" %%d in ('echo !workingdate!') do (
    Set %%a=%%d
    Set %%b=%%e
    Set %%c=%%f
)
 
SHIFT
 
Set /a d=1!d!%1
Set m=1!m!
CALL :FixDay
CALL :Get_DateOutputFormat
 
Set DATEADD=!DateOutputFormat:d=%d%!
ECHO !DateOutputFormat! | Find /i "yy">NUL
IF NOT ERRORLEVEL 1 (
    Set DATEADD=!DATEADD:yy=%y:~-2%!
) ELSE (
    Set DATEADD=!DATEADD:y=%y%!
)
 
Set /a MonthNum=1!m!-100
ECHO !DateOutputFormat! | Find /i "mmm">NUL
IF NOT ERRORLEVEL 1 (
    for /f "tokens=%MonthNum% delims=, " %%m in ('echo %monthnames%') do Set DATEADD=!DATEADD:mmm=%%m!
) ELSE (
    Set DATEADD=!DATEADD:m=%m%!
)
GOTO :EOF
 
 
:FixDay
If !d! LEQ 100 (
    Set /a M-=1
    if !M! == 100 (
        Set /a Y-=1
        Set M=112
    )
    Set MCounter=101
    Set DaysInThisMonth=
 
    for %%d in (%DaysInMonth%) do if not defined DaysInThisMonth if !MCounter! == !M! (Set DaysInThisMonth=%%d) else (Set /a MCounter+=1)
 
    if !M! == 102 (
        if %Y:~-2% == 00 (
            Set /a Mod=!Y:~0,2! %% 4
            if !Mod! == 0 Set /a DaysInThisMonth+=1
        ) else (
            Set /a Mod=!y! %% 4
            if !Mod! == 0 Set /a DaysInThisMonth+=1
        )
    )
    Set /a D+=!DaysInThisMonth!
) ELSE (
    Set MCounter=101
    Set DaysInThisMonth=
 
    for %%d in (%DaysInMonth%) do if not defined DaysInThisMonth if !MCounter! == !M! (Set DaysInThisMonth=%%d) else (Set /a MCounter+=1)
    if !M! == 102 (
        if %Y:~-2% == 00 (
            Set /a Mod=!Y:~0,2! %% 4
            if !Mod! == 0 Set /a DaysInThisMonth+=1
        ) else (
            Set /a Mod=!y! %% 4
            if !Mod! == 0 Set /a DaysInThisMonth+=1
        )
    )
  
    If !d! GTR 1!DaysInThisMonth! (
        Set MCounter=101
        Set DaysInThisMonth=
 
        for %%d in (%DaysInMonth%) do if not defined DaysInThisMonth if !MCounter! == !M! (Set DaysInThisMonth=%%d) else (Set /a MCounter+=1)
        if !M! == 102 (
            if %Y:~-2% == 00 (
                Set /a Mod=!Y:~0,2! %% 4
                if !Mod! == 0 Set /a DaysInThisMonth+=1
            ) else (
                Set /a Mod=!y! %% 4
                if !Mod! == 0 Set /a DaysInThisMonth+=1
            )
        )
 
        Set /a D-=!DaysInThisMonth!
 
        Set /a M+=1
        if !M! == 113 (
            Set /a Y+=1
            Set M=101
        )
    )        
) 
 
If !d! LEQ 100 goto FixDay
Set MCounter=101
Set DaysInThisMonth=
 
for %%d in (%DaysInMonth%) do if not defined DaysInThisMonth if !MCounter! == !M! (Set DaysInThisMonth=%%d) else (Set /a MCounter+=1)
if !M! == 102 (
    if %Y:~-2% == 00 (
        Set /a Mod=!Y:~0,2! %% 4
        if !Mod! == 0 Set /a DaysInThisMonth+=1
    ) else (
        Set /a Mod=!y! %% 4
        if !Mod! == 0 Set /a DaysInThisMonth+=1
    )
)
If !d! GTR 1!DaysInThisMonth! goto FixDay
 
Set m=!m:~-2!
Set d=!d:~-2!
GOTO :EOF
 
:Function_GetOutputAsCurrentFormat
Set GetOutputAsCurrentFormat=!CurrentDateFormat:d=%d%!
Set GetOutputAsCurrentFormat=!GetOutputAsCurrentFormat:y=%y%!
Set GetOutputAsCurrentFormat=!GetOutputAsCurrentFormat:m=%m%!
)
GOTO :EOF
 
:GetCurrentDateFormat
for /f "Tokens=3" %%a in ('reg query "hkcu\control panel\international" /v sShortDate ^| find /i "sShortDate"') do Set sShortDate=%%a
for /f "Tokens=3" %%a in ('reg query "hkcu\control panel\international" /v sDate ^| find /i "sDate"') do Set sDate=%%a
 
Set CurrentDateFormat=!sShortDate:%sDate%=/!
Set CurrentDateFormat=!CurrentDateFormat:MM=M!
Set CurrentDateFormat=!CurrentDateFormat:dd=d!
Set CurrentDateFormat=!CurrentDateFormat:yyyy=y!
GOTO :EOF
 
:Get_DateOutputFormat
if not defined DateOutputFormat Set DateOutputFormat=!CurrentDateFormat!
ECHO !DateOutputFormat! | Find /i "mmm">nul
IF ERRORLEVEL 1 Set DateOutputFormat=!DateOutputFormat:MM=M!
Set DateOutputFormat=!DateOutputFormat:dd=d!
Set DateOutputFormat=!DateOutputFormat:yyyy=y!
GOTO :EOF

Open in new window

0
 
LVL 39

Expert Comment

by:BillDL
ID: 23695492
Well, my humble offering pales into insignificance when it ends up sharing a page with a script like that.   It will take me some days (perhaps weeks) to digest it all.  It's a great pity purpleoak managed to use a bit of my suggestion and integrate it into his own script(s) before seeing your full version.

purpleoak

If copying and pasting AmazingTech's code from the Code Snippet to Notepad (XP version and maybe others), I suggest you disable word wrap, then enable it again.  There's something strange happened in the remarked out portions where asterisks are visible, and it's probably due to the formatting in the Code Snippet rather than Notepad itself.  Hard to tell, but easy enough to fix with a bit of backspacing.  It'as only the comments that are affected.

Maybe you should consider asking for the question to be re-opened if you find that AmazingTech's code is really useful to you and need any continued clarification.

Thanks for sharing that after the question was closed AmazingTech.  Brilliant as always :-)
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23696472
I saw the other question that purpleoak posted but qlemo was pretty quick on that one.

This has many things added to it like able to add days and not dependant on the date format on your computer as long as you are using numbers for the month.
0
 
LVL 39

Expert Comment

by:BillDL
ID: 23698766
He, he.  Yes, I see it now.

http://www.experts-exchange.com/Q_24162133.html
purpleoak:
      "... Is it possible to do this in a batch file?"
Qlemo:
      "It is. I prefer the 'AmazingTech way'."

Good one :-)
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question