Normal Topic WORKING WITH DATES (Read 1217 times)
NHUser
Full Member
***
Offline



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
WORKING WITH DATES
May 3rd, 2013 at 12:19pm
Print Post Print Post  
I have an application where I enter two dates.  I want to be able to calculate the number of days between the two dates, but I don't want to include Sat or Sun or Holidays.  Is there a way to program a calculation to remove these days without manually manipulating the data?

  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: WORKING WITH DATES
Reply #1 - May 3rd, 2013 at 4:37pm
Print Post Print Post  
This is possible. What you would do is set a date variable equal to the most recent date, then loop backwards to the oldest date decrementing the variable. Check the date against a list of holidays, perhaps in an external database, and against the day of the week. If it is not a weekend or a holiday, increment an int variable. At the end of the date loop the int variable will hold the number of non-holiday weekdays between the two dates.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: WORKING WITH DATES
Reply #2 - May 3rd, 2013 at 5:20pm
Print Post Print Post  
Less simple, but also more efficient than Ray's solution: you could calculate the number of weeks between the two dates then multiply that by 5, giving the number of weekdays. Then using a list of holiday dates, count the number of weekday holiday days between the two dates and subtract that from the total weekdays.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
NHUser
Full Member
***
Offline



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
Re: WORKING WITH DATES
Reply #3 - May 7th, 2013 at 2:43pm
Print Post Print Post  
Hopefully, the difference between the two dates will be measured in days and not weeks.  Our goal is to complete the task in three working days or less.  I need to determine if the difference between the two dates includes weekend or holidays so I can delete them from my calculation.  So it seems that Ray's suggestion is probably best.

That being said, Ray, would it be possible for you to show me a quick example of how to do what you're suggesting?
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: WORKING WITH DATES
Reply #4 - May 7th, 2013 at 3:51pm
Print Post Print Post  
NHUser, my approach does not "measure in weeks". It simply calculates the number of weeks between two dates and uses that to determine the number of weekdays. The end result is in days, not weeks.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: WORKING WITH DATES
Reply #5 - May 7th, 2013 at 10:15pm
Print Post Print Post  
I only tested this a little, but this should give you the number of weekdays between any two dates.  Its doesn't do the holidays, yet. You might also want to make sure end date is after start start, etc:

Code
Select All
function WorkDays(start_date as date, end_date as date) as int
var days as int
var dd as date
var sub1 as int
var sub2 as int
var weeks as int
var the_dow as int
var remain as int

sub1 = 0
the_dow = @dow(start_date)
if(the_dow = 6)  // start is Saturday, advance two days
{
        sub1 = 2
}
else if(the_dow = 7) // start is Sunday, advance one day
{
        sub1 = 1
}
sub2 = 0
the_dow = @dow(end_date)
if(the_dow = 6) // end is Saturday, go back one day
{
        sub2 = 1
}
else if(the_dow = 7) // end is Sunday, go back two days
{
        sub2 = 2
}

// Calc total days
days = (end_date - sub2) - (start_date + sub1)

// Add one to make end date inclusive
days = days + 1

// Calc total weeks as an integer
weeks = days / 7

// Get the modulo in case it isn't an even number of weeks
remain = @mod(days, 7)

// Calc weekdays (5 in a week, plus any portion of a week left over)
days = (weeks * 5) + remain

return(days)
end function
 

  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
NHUser
Full Member
***
Offline



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
Re: WORKING WITH DATES
Reply #6 - May 8th, 2013 at 12:33pm
Print Post Print Post  
Thanks for the help.

What I meant by my earlier comment is that the two dates in my file, in most cases, will be less than a week apart so it would be hard to use the weeks calculation.
  
Back to top
 
IP Logged
 
NHUser
Full Member
***
Offline



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
Re: WORKING WITH DATES
Reply #7 - May 8th, 2013 at 7:13pm
Print Post Print Post  
Using hints from your example, I've been able to develop a for-next loop that checks the dates and "doesn't count" Sat and Sun.  My programming is below.

I'm now stuck trying to check a file of Holidays.  I've created a @xlu statement to see if the date is present in the holiday file.  Within the holiday file for each date there is an integer under the element VALID.  So, I expect the date to search in the Holiday file and for the vHolidayCheck variable to be 1 if there is a date.

Unfortunately, even when I know the date is present in the Holiday file, I'm not getting the 1 back.  Is there some trick to comparing dates from another file?  I've tried setting the date formats the same but that doesn't seem to work.

Please advise.



var vS as int
var vCount as int
var vdays as int
var vStartDate as date
var vCountDate as date
var vEndDate as date
var vHolidayCheck as int

vStartDate=OrderReceivedDate
vEndDate=BookDate

For vS = 0 to 20
     
     If vS=0
     {
           vCount=1
           VCountDate=vStartDate
     }
     else
     {
           vCountDate=vStartDate+vs
           
           If @dow(vCountDate)>5
           {
                 
                 vCount=vCount
           }
           Else
           {
                 vCount=vCount+1
           }
     }
     
// check if the date being checked is a holiday from the Holiday file
     
     vHolidayCheck = @XLU(@fn,vCountDate,"Holidays!Holidays","valid")
     
     writeln(vCountDate,vHolidayCheck)      

     if vHolidayCheck=1
     {
           vCount=vCount-1
     }


// check if the date being checked is the end date

     If vCountDate=vEndDate
     {
           
           vS=20
     }      

     TimeToEnter=vCount
Next
  
Back to top
 
IP Logged
 
NHUser
Full Member
***
Offline



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
Re: WORKING WITH DATES
Reply #8 - May 8th, 2013 at 9:10pm
Print Post Print Post  
It was programmer error!!

The element name is Holiday, not Holidays.  My program is working fine, now.  Thanks for all your help!
  
Back to top
 
IP Logged