Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Calculating Hours Between Two Dates (Minus Weekends!)?

Calculating Hours Between Two Dates (Minus Weekends!)?
Thread Tools
marusin
Senior User
Join Date: Sep 2000
Location: Illinois
Status: Offline
Reply With Quote
Dec 16, 2002, 01:16 PM
 
Hey all,

I'm trying to figure out an approach to this and am having problems...

I have a form that has two dates that a user is entering (a start date and end date for contractors). Right now I'm using the Date.getTime() function to get the millisecond value and am figuring out the difference and in turn getting the amount of hours between the two dates. Now we need to figure out the hours but take in to account that the workers won't be working on Saturdays and Sundays.

Anyone know a good way to figure out the hours between the two dates taking into account the weekends? I'm trying to think of a way I can figure out how many weeks are between the two dates and then subtract out the 16 hours per week of weekends (8 hours a day is the multiplier), but things get tricky to figure out the exact weeks...

Any help would be great! Thanks!
     
sambeau
Mac Elite
Join Date: Jun 2001
Location: Dundee, Scotland
Status: Offline
Reply With Quote
Dec 16, 2002, 01:27 PM
 
Originally posted by marusin:
Right now I'm using the Date.getTime()
Is this totaly client-side? ie are you/could you connect to a mySQL database?

If not, first find out how many days there are between the two events, then work out how many days until the weekend at the beginning. Then work out how many days after the last weekend at the end. ie if mon at start and wed at end you will need to add 5 days at start and 3 days at end. So cut 8 days of the number of days you have and then divide by seven and multiply by five and the add those eight days back on.

viola! you now have the correct number of days minus the weekens..

s.
( Last edited by sambeau; Dec 16, 2002 at 01:33 PM. )
     
marusin  (op)
Senior User
Join Date: Sep 2000
Location: Illinois
Status: Offline
Reply With Quote
Dec 16, 2002, 01:30 PM
 
This is completely client side... Thanks for the help.. I'm going to take the approach you said and see what happens...

Originally posted by sambeau:


Is this totaly client-side? ie are you/could you connect to a mySQL database?

If not, first find out how many days there are between the two events, then work out what day it is today. If you divide by 7 you'll obviously get the number of weeks. The hard bit is removing the weekends first.

How about this:

I would count how many days until the weekend at the beginning. Then work out how many days after the last weekend at the end. ie if mon at start and wed at end you will need to add 5 days at start and 3 days at end. So cut 8 days of the number of days you have and then divide by seven and multiply by five and the add those eight days back on.

viola! you now have the correct number of days minus the weekens..

s.
     
sambeau
Mac Elite
Join Date: Jun 2001
Location: Dundee, Scotland
Status: Offline
Reply With Quote
Dec 16, 2002, 01:36 PM
 
sorry about that just edited my message after you posted to try and make it make sense.

So Javascript then?

Still the same technique. Strip the period down to exact weeks, keeping the spare days to add on at the end. Then divide by seven to get weeks (it will obviously be a whole number) then multiply that back by 5 to get work-days. Then add the extra days back on.

Anyone got a fancy mathmo way?
     
marusin  (op)
Senior User
Join Date: Sep 2000
Location: Illinois
Status: Offline
Reply With Quote
Dec 16, 2002, 04:37 PM
 
Thanks for your help, Sambeau... Here's my JavaScript for anyone that's interested:
Code:
function getHoursBetweenDates() { var theForm = document.forms[0]; var beginDate = new Date(theForm.StartDateLI.value); var endDate = new Date(theForm.EndDateLI.value); var getWorkDay; //this variable will hold the workday multiplier selected on the form... Either 8 hours a day or 4 hours a day //Get the Millisecond Values of the Start Date and End Dates var startTime = beginDate.getTime(); var endTime = endDate.getTime(); //Need to validate Dates //Validate that End Date is after the Start Date if (startTime > endTime) { alert("The Start Date is later than the End Date"); } else { var differenceInSeconds = (endTime - startTime); var daysDifference = (differenceInSeconds / (1000 * 60 * 60 * 24)); //now get the WorkDay value to use as a multiplier if (theForm.WorkDay[0].checked) { getWorkDay = theForm.WorkDay[0].value; } else if (theForm.WorkDay[1].checked) { getWorkDay = theForm.WorkDay[1].value; } var theWeekends = determineWeekends(beginDate,endDate); var theDays = (daysDifference - theWeekends) / 7; var daysLessWeekends = (theDays * 5) + theWeekends; //we multiply theDays * 5 (normal work week) and then add back the original days var roundedDaysLessWeekends = roundDays(daysLessWeekends); //call the roundDays function that will only take the hours to the left of the decimal point //Set the ProjectedHours field with the rounded hours (taking weekends into account) theForm.ProjectedHours.value = getWorkDay * roundedDaysLessWeekends; } } function determineWeekends(begin,end) { var beginDay = begin.getDay(); var endDay = end.getDay(); var determineStart; var determineEnd; determineStart = 6 - beginDay; //Subtract beginning day from 6 (which is considered Saturday) determineEnd = endDay; //We just take the value of the EndDay because we check after the last weekend return determineStart + determineEnd; } function roundDays(allDays) { var theString = new String(allDays); var locateDecimal = theString.indexOf('.'); var extractDays = theString.substring(0, locateDecimal); return extractDays; }
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 03:38 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,