Discussion:
Working with dates in VB6
(too old to reply)
R C Nesbit
2013-02-24 19:56:28 UTC
Permalink
Problem:

Plant hire - the plant is hired by the day, but the hire
charge is by the week.
The hire charge is actually levied by the day, but only
Monday to Friday, i.e. if it is 100/week the charge is
20/day

Records hold an item of plant, the weekly rate, the date on
hire, and the date off-hire

The application looks at the record and the default date
off-hire is 1 day *before* the date on hire, unless the
hire is ended, when the off-hire date is set.

So the app must calculate the cost so far, or the cost from
on-hire to off-hire if the off-hire is changed to a data
after the on-hire date.

Problem is working out the cost for the on-hire days, only
accounting for Mon to Fri over days or weeks.

My solution is a function CalcHireCost, passed paramaters
on-hire(FDate), off-hire(TDate), and Rate

then I have:

iDays = DateDiff("d", FDate, TDate, vbMonday)
iSDay = 0
For i = 1 To iDays
If Weekday(DateAdd("d", i, FDate), vbMonday) < 6 Then
iSDay = iSDay + 1
End If
Next i

CalcHireCost = (iSDay * (Rate / 5))

Can anyone improve on this or see any logic-bombs in it?
--
Rob Pearson
Dr J R Stockton
2013-02-25 17:10:45 UTC
Permalink
Post by R C Nesbit
Plant hire - the plant is hired by the day, but the hire
charge is by the week.
The hire charge is actually levied by the day, but only
Monday to Friday, i.e. if it is 100/week the charge is
20/day
Records hold an item of plant, the weekly rate, the date on
hire, and the date off-hire
The application looks at the record and the default date
off-hire is 1 day *before* the date on hire, unless the
hire is ended, when the off-hire date is set.
So the app must calculate the cost so far, or the cost from
on-hire to off-hire if the off-hire is changed to a data
after the on-hire date.
Problem is working out the cost for the on-hire days, only
accounting for Mon to Fri over days or weeks.
My solution is a function CalcHireCost, passed paramaters
on-hire(FDate), off-hire(TDate), and Rate
iDays = DateDiff("d", FDate, TDate, vbMonday)
iSDay = 0
For i = 1 To iDays
If Weekday(DateAdd("d", i, FDate), vbMonday) < 6 Then
iSDay = iSDay + 1
End If
Next i
CalcHireCost = (iSDay * (Rate / 5))
Can anyone improve on this or see any logic-bombs in it?
You only need Weekday to get the day-of-week of the first day; ISO 8601
week numbers follow a simple 1-7 1-7 sequence, rolling over on Sunday
night.

While there are more than 6 days left, you can just add 7 to the day
count and 5 to the work-day count.

Better still, use integer division of the date difference to get the
number of 7-day units, ..., and short-circuit the previous paragraph.

But is hire charged for Official Holidays, such as Good Friday, Easter
Monday, etc.?
--
(c) John Stockton, nr London, UK. E-mail, see Home Page. Turnpike v6.05.
Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
Loading...