473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

working hours between two times

Hi

I need to be able to calculate the working time between a StartDate and an EndDate. the data is in dd/mm/yyyy hh:mm:ss format and given an 8.5 hr day (weekdays). Ive found lots of really useful stuff on this list but nothing that really does it for me. I've have used a bit of code that lets me know the days

Public Function GetNumberOfWorkDays(sStartDate, sEndDate)
Dim iDays
Dim iWorkDays
Dim sDay
Dim i

iDays = DateDiff("d", sStartDate, sEndDate)

iWorkDays = 0

For i = 0 To iDays
'First day of the week is sunday
sDay = Weekday(DateAdd("d", i, sStartDate))
If sDay <> 1 And sDay <> 7 Then
iWorkDays = iWorkDays + 1
End If
Next
GetNumberOfWorkDays = iWorkDays
End Function

(thanks again to you guys) but i think that i need to think about this radically different (and beyond my knowledge) to get what i need. Has anyone come across anything like this before and if so would you be able to help me out (and more to the point annoy my manager who dropped this on me in the first place) I have to confess that its been 6 years since i last used access in anger please forgive me.

Nick
Oct 27 '06 #1
14 11066
NeoPa
32,556 Expert Mod 16PB
Do you have some example data?
Can you express precisely what you're after?
In the form :-
Working hours are from 09:00 to 13:00 and from 14:00 to 18:30 on weekdays.
I need to determine, given two date/times, how much working time has elapsed between them.

This is just my guess, of course. Is your requirement similar to this?
Oct 27 '06 #2
Do you have some example data?
Can you express precisely what you're after?
In the form :-
Working hours are from 09:00 to 13:00 and from 14:00 to 18:30 on weekdays.
I need to determine, given two date/times, how much working time has elapsed between them.

This is just my guess, of course. Is your requirement similar to this?
No worries and thanks for responding

working hours are between 08:30 to 17:00 - no breaks - its a call centre :-)
yes the requirement is similar. I'm looking at call data so if StartDate = 11/09/2006 09:30:00 & EndDate is 26/09/2006 11:30:15 there are 11 days 2 hours and 15 seconds of working time between opening and closing calls.

hope this makes it clearer
Nick
Oct 27 '06 #3
NeoPa
32,556 Expert Mod 16PB
Crystal!

Unfortunately the problem itself is quite involved.
I'll look at producing something for you but I'm at work ATM and something of this size will take some time.
Oct 27 '06 #4
Anything you can do would be much appreciated thank you. There is no major panic as i'm being sent off on another adventure for the next 10 days anyway so at least i get some breathing space :-)

Thanks again for responding and in advance for any help you can offer.

Nick
Oct 27 '06 #5
NeoPa
32,556 Expert Mod 16PB
This doesn't seem to work for all possible combinations of dates but I think it's close.
BTW does anyone know a way of extracting the integer part of a number without rounding it in Access VBA?

Expand|Select|Wrap|Line Numbers
  1. Public Function WorkHours(datStart As Date, datEnd As Date) As Date
  2.     Dim lngDays As Long, lngStartDay As Long, lngEndDay As Long
  3.     Dim datStartTime As Date, datEndTime As Date, datTime As Date
  4.  
  5.     'Dates stored as integral days with the time as a fractional portion
  6.     lngStartDay = CLng(Left(Format(datStart, "0000000000.0000000000"), 10))
  7.     lngEndDay = CLng(Left(Format(datEnd, "0000000000.0000000000"), 10))
  8.     datStartTime = datStart - lngStartDay
  9.     datEndTime = datEnd - lngEndDay
  10.     If datStartTime < #8:30:00 AM# Then datStartTime = #8:30:00 AM#
  11.     If datStartTime > #5:00:00 PM# Then
  12.         lngStartDay = lngStartDay + 1
  13.         datStartTime = #8:30:00 AM#
  14.     End If
  15.     If datEndTime > #5:00:00 PM# Then datEndTime = #5:00:00 PM#
  16.     If datEndTime < #8:30:00 AM# Then
  17.         lngEndDay = lngEndDay - 1
  18.         datEndTime = #5:00:00 PM#
  19.     End If
  20.     If (CInt(Format(datStart, "w")) Mod 7) < 2 Then
  21.         lngStartDay = lngStartDay + 3 - CLng(Format(datStart, "w"))
  22.         datStartTime = #8:30:00 AM#
  23.     End If
  24.     If (CInt(Format(datEnd, "w")) Mod 7) < 2 Then
  25.         lngEndDay = lngEndDay - CLng(Format(datEnd, "w"))
  26.         datEndTime = #5:00:00 PM#
  27.     End If
  28.     datStart = lngStartDay + datStartTime
  29.     datEnd = lngEndDay + datEndTime
  30.     WorkHours = 0
  31.     If (lngEndDay - lngStartDay) > 7 Then
  32.         lngDays = ((lngEndDay - lngStartDay) \ 7) * 5
  33.         WorkHours = WorkHours + lngDays
  34.         lngEndDay = lngEndDay - ((lngDays / 5) * 7)
  35.     End If
  36.     If Format(lngEndDay, "ww") > Format(lngStartDay, "ww") Then
  37.         WorkHours = WorkHours + 5
  38.         lngEndDay = lngEndDay - 7
  39.     End If
  40.     datTime = datEndTime - datStartTime
  41.     If datEndTime < datStartTime Then
  42.         datTime = datTime + #8:30:00 AM#
  43.         WorkHours = WorkHours - 1
  44.     End If
  45.     WorkHours = WorkHours + _
  46.                 (CInt(Format(lngEndDay, "w")) Mod 7) - _
  47.                 (CInt(Format(lngStartDay, "w")) Mod 7) + _
  48.                 datTime
  49. End Function
Oct 28 '06 #6
Another approach is to use a loop.

Expand|Select|Wrap|Line Numbers
  1.  
  2. dim NoOfDays as long
  3. Dim NextDay as date
  4. Dim hrs as single
  5.  
  6.          NextDay = StartDate
  7.  
  8. AddADay:
  9.  
  10.          hrs = hrs * 8.5
  11.  
  12.  
Oct 28 '06 #7
Another approach is to use a loop.

Expand|Select|Wrap|Line Numbers
  1.  
  2. dim NoOfDays as long
  3. Dim NextDay as date
  4. Dim hrs as single
  5.  
  6.          NextDay = StartDate
  7.  
  8. AddADay:
  9.  
  10.          if weekday(nextday) > 1 and weekday(nextday) < 7 then hrs = hrs * 8.5
  11.          nextday = dateadd("d",1,nextday)
  12.          if nextday <= enddate then goto AddADay
  13.  
  14.  
  15.  
Oct 28 '06 #8
NeoPa
32,556 Expert Mod 16PB
I admit that I sometimes I don't see the wood for the trees.
Normally going straight for an answer arithmetically is my preferred approach rather than looping etc (probably because when I learned my trade instructions executed at a rate of some hundreds per second - don't think I'm exaggerating).
In this case, with most data resulting in only a few iterations anyway and processors running at some billions per second (for simpler instructions at least) this is absolutely not an issue.
The fact that I goofed up the code somewhere too doesn't help :-(.

Let me see if I can redeem myself a little then :-
As the Weekday() function is used (another thing I missed) you can use the Weekday([DateField],vbSaturday) version to start the week on Saturday, which makes testing for a weekend date a little easier.

I shall now go off and kick myself for a while for wasting so much time on the earlier heap of...
Oct 28 '06 #9
MMcCarthy
14,534 Expert Mod 8TB

BTW does anyone know a way of extracting the integer part of a number without rounding it in Access VBA?
Fix(24.78) will return 24
Oct 28 '06 #10
NeoPa
32,556 Expert Mod 16PB
Thanks M.

Int() and Fix().
I confused CInt() with Int() when I was looking.
Oct 28 '06 #11
Many thanks to all you guys.

Have just returned from my managers last whim to see that you have given me loads of things to try.

great to see that there are people out there who want to help. I do hope that i can contribute something back to the board in the future.

Nik
Nov 10 '06 #12
iceone
2
Hi everybody,



I am monitoring a call center and i need to calculate the working hours between two times.
I just started with access as the dataset has become too big to be handled in ecxel.
i managed to calculate the working day difference (ie excluding sundays, saturdays, thanksgiving, ecc,) but i'm stuck with the hours...

have u already built a module for this purpose?

thank u all

PS i consider as working hours from 09;00 am to 19:30 pm (monday-fryday)
Jul 4 '08 #13
NeoPa
32,556 Expert Mod 16PB
You need to post your question in a new thread if you want help.

PS. You may link between the threads if you like (either way or even both).
Jul 7 '08 #14
youmike
69
This thread asks an almost identical question to another I've just commented on. Here's my view:

Basically, there is really no reliable alternative to purpose built code which takes start date & time, end date & time and checks each successive date for day of the week and then in a table which lists the holidays for your particular location. It's not an uncommon need, but because different countries have different holidays, some of which (Easter, for instance - different in Greece from Italy) change actual date from year to year, I doubt that an of the shelf global solution exists.

Sorry to be so negative.
Jul 8 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Monty | last post by:
Let's say you provide an online service from 7:00AM to 6:00PM Eastern Time (daylight time in the summer). Is there way of showing these hours of availability on a web page in the user's local...
4
by: John Smith | last post by:
Hi, I'm having trouble working out the best way of calculating the time difference between two times on the same day. The example I have found does return the hours (in this case 8) but forgets...
1
by: sg_s123 | last post by:
============================================================================ 02-Feb-04 03-Feb-04 Staff Staff 0800hr- 1300hr- 1700hr- 1900hr- 0800hr- 1300hr- 1700hr- 1900hr- Number...
2
by: Trevor | last post by:
Argh! This problem is driving me nuts! Can you help? In November of 2003, I installed a web service on Windows Server 2003 built in VB.NET for v1.1.4322 of the framework. It contains a timer...
22
by: Drum2001 | last post by:
I have a table that tracks employee times. I have a column (Date/Time). Users, through a form, enter how long it takes them to complete a task. For example, 03:45 = 3 hours and 45 mins. I am...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
3
by: Nigel Heald | last post by:
Hi Folks, We have a form that records flight times in hours and minutes, for example a 1 hour 15 minute flight is recorded as 1:15 Does anyone know how to get Access 2003 to calculate a total...
4
by: NormAmst | last post by:
I have a list of CPU processing times and job durations for an entire department at work. There are 3 classifications I am maintaining. CPU time during peak hours , CPU time during non peak hours...
7
by: Tom | last post by:
By my estimate, greater than 90% of the online doco code does not work without additional coding effort. Fully working solutions are invaluable for the student. A guru's work measured in minutes...
9
by: mgrubbs | last post by:
#include <iostream> using std::cout; using std::cin; using std::endl; int main() { int code = 0; int charges = 0;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.