Number of Days Between Dates

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Eric D
Junior Coder
Posts: 27
Joined: Thu Sep 19, 2002 6:08 pm
Location: Orange County, CA

Number of Days Between Dates

Post by Eric D » Wed Feb 05, 2003 7:39 pm

I am working on a project to evaluate the difference between dates. I am designing a macro to do this but it is proving to be difficult. I will post my code when I am done.

If anyone has done this I would appreciate the help.

01/15/2003 - 12/31/2002 = 15

Thanks
Eric Dye

Lumumba

Post by Lumumba » Wed Feb 05, 2003 9:45 pm

Hmm, in Microsoft's Excel a date will be converted into a numeric value which could be used for date calculation. I'm shure VBScript is the best way to get this solved.

Eric D
Junior Coder
Posts: 27
Joined: Thu Sep 19, 2002 6:08 pm
Location: Orange County, CA

Post by Eric D » Wed Feb 05, 2003 9:57 pm

I am determined to do it in Macro Scheduler. I am close right now but I still need to work on it.

Eric

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Feb 06, 2003 5:29 am

The current of Macro Scheduler has VB built in. The following Macro Scheduler script calculates the difference between two dates.
=======================

VBSTART
VBEND

Let>Date1=01/15/2003
Let>Date2=12/31/2002

VbEval>DateDiff("d","%Date2%","%Date1%"),Span

Message>Difference is %Span% days.
================================

To do this without DateDiff, you will have to break the dates into years, months, days, calculate the differences between each interval, and add the totals. This will require a number of If> statements to allow for 28,30 and 31 day months. You could use 28 as a basis and then add 2 or 3 based on the actual month.
You could do a number of steps:
1. First parse the date into numbers for days, months, years.
2. How many days till end of oldest month?
3. How many days into the newest month? Add to #2.
4. How many months in between oldest and newest month? Add 28 to #3 for each month.
5. How many of those months have 30 days?. Add 2 to #4 for each 30 day month.
6. How many of those months have 31 days?. Add 3 to #5 for each 31 day month.
7. Do similar routine for years, how many years between oldest year and newest year? Add 365 to #6 for every year and add 1 more for each leap year.

Is it doable? yes......
Is it worthwhile? Happy programming, I will use the built in VB.

Enjoy
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Feb 06, 2003 5:55 am

I made a slight change to the calculation line to give you the absolute value of days, making it unnecessary to worry about the sequence. You will always get a positive value.

Changes are in bold

VbEval>Abs(DateDiff("d","%Date2%","%Date1%")),Span
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Eric D
Junior Coder
Posts: 27
Joined: Thu Sep 19, 2002 6:08 pm
Location: Orange County, CA

Post by Eric D » Thu Feb 06, 2003 6:14 pm

Bob,

Thanks for the VB Script I will probably go that route but...

I also figured out another way to do the date differnce in days inside of Macro Scheduler. There is a bunch of code in the example but it is not all used to figure out the difference, still more than VBscript but not as bad as you thought.

The Sub and Add functions can handle dates and they work quite well. The meat of this operation is:

Get your 2 dates (one being today and the other is your date to compare)
Check to see if they are equal
if not deduct 1 day from todays date and check equality again
count the number of times you have to loop through until the dates are equal. That is the difference in Days


Thanks Again,
Eric Dye

Begin Script Example

Let>MaxDaysOld=13

GetFileList>%FidelityPath%*.*,FidFiles
Separate>FidFiles,;,file_names
MessageModal>Num Files: %file_names_count%

Let>k=0
Repeat>k
Let>k=k+1
FileDate>file_names_%k%,Fid_File_Date

MidStr>%Fid_File_Date%,1,4,File_Year
MidStr>%Fid_File_Date%,5,2,File_Mo
MidStr>%Fid_File_Date%,7,2,File_Day

Concat>File_Mo,/
Concat>File_Day,/
Concat>File_Mo,%File_Day%
Concat>File_Mo,%File_Year%
Let>Final_File_Date=%File_Mo%
Let>Diff=0

GetDate>Today

Label>FindDiff
If>%Final_File_Date%=%Today%,Diff_Is,Diff_Chk


Label>Diff_Is
If>%Diff%>%MaxDaysOld%,DelFile,DoneDiff

Label>Diff_Chk
'messagemodal>%Diff%
MessageModal>%Today%
'MessageModal>%Final_File_Date%


Sub>%Today%,1
Add>Diff,1
Goto>FindDiff

Label>DelFile
DeleteFile>file_names_%k%

Label>DoneDiff
MessageModal>Final Difference was %Diff%

Until>k,file_names_count

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts