Date Class is lacking

[quote=17087:@Tim Hare]“much more code”?? One or two lines at most.

dim theDate as New Date
dim tomorrow as New Date(theDate)
tomorrow.Day = tomorrow.Day + 1
dim yesterday as New Date(theDate)
yesterday.Day = yesterday.Day - 1[/quote]

An you could shorten it still to 4 lines of code

Dim tomorrow as New Date
Dim yesterday as New Date

tomorrow.totalseconds = tomorrow.totalseconds + 86400
yesterday.totalseconds = yesterday.totalseconds - 86400

As in, d.AddBusinessDays( 7 ) would bring you from Monday to the following Wednesday (9 days), or something else?

Just as you said. Some implementations allow for scheduling holidays and such.

I guess a simple, holiday ignorant solution, would be as easy as:

Sub AddBusinessDays(Extends d As Date, count As Integer) For i As Integer = 1 To count d.Day = d.Day + 1 If d.DayOfWeek = 7 Then d.Day = d.Day + 2 End If Next End Sub

Can you point me towards an existing implementation in some other language?

Hm, we rolled our own for Tcl, but here is an example use in php: http://www.php.net/manual/en/datetime.formats.relative.php. You can add things such as 3 weekdays, 4 sundays, etc… Here is a gem for ruby: GitHub - bokmann/business_time: Support for doing time math in business hours and days

I’d calculate it up front since you know that every 5 days would require 2 more. Without testing, something like this…

count = count + d.DayOfWeek - 2
d.Day = d.Day - d.DayOfWeek + 2  // I'm looking to bring it back to the Monday so this math might be off
diff = count \\ 5
count = count + ( diff * 2 )
d.Day = d.Day + count

Then the question is how to deal with the case when d falls on a weekend. I’ll take a look at those links later and maybe build it into my module. I’ll report back either way.

This calls for a subclass of Date, I think. That subclass would have properties to hold holidays and business start hour/end hour, and work week start day/end day, then implement things like BusinessDay, BusinessHour, BusinessMinute, etc. My thinking is that a date that lands on a non-work date (weekend or holiday) would automatically be incremented to the next business date. So, for example, if you did something like this:

dim d as Date = SomeSaturday
dim bd as new BusinessDate( d ) // Would give you the following Monday

An interesting exercise…

[quote=17188:@Kem Tekinay]This calls for a subclass of Date, I think. That subclass would have properties to hold holidays and business start hour/end hour, and work week start day/end day, then implement things like BusinessDay, BusinessHour, BusinessMinute, etc. My thinking is that a date that lands on a non-work date (weekend or holiday) would automatically be incremented to the next business date.

An interesting exercise…[/quote]

That makes sense, and yeah, things like this are fun!

Off the top of my head it seems pretty simple, maybe I’m missing the question

Dim d As New Date

For Counter As Integer = 1 to NoOfBusinessDays

d.Day = d.Day + 1
If d.DayOfWeek = 1 or d.DayOfWeek = 7 then Counter = Counter - 1

Next Counter

Visual Foxpro has one of the most robust list of date/time functions that I’ve ever seen.

link text

If you wanted to advance, say, 100 business days, I’d expect that to take much longer than the method I proposed. With 100 days, you know you’d have to add around 40 additional days for the weekends, so you wouldn’t need to loop.

A loop would take longer than just performing the math up front.

100/7 * 2 is around 28 I wish there were 40 weekend days in 100 days :slight_smile:

This forum is suddenly freaking out on me…

darn you said business days LOL

It would be 100 \ 5 , not 7. You want to advance it by 100 business days, so that’s 20 five-day weeks. Two weekend days for each of those = 40 additional days. So 100 business days = 140 days.

I’m not getting the last posts here, so time to clear my cache and/or cookies, I think.

OK, I can’t see the last post in this conversation until I or someone else posts something else. Then the formerly last post appears, but the new one does not.

I reported this here:

https://forum.xojo.com/2496-can-t-see-last-post

[quote=17240:@John Fatte]Visual Foxpro has one of the most robust list of date/time functions that I’ve ever seen.

link text[/quote]

I don’t see anything there you can’t easily do in RB, subclass a date and add those things, less then an hour and you’ll have it for life.

I wrote a program called CleanHaven. One of its many functions is to determine the ‘Number of days between two dates’. There is a popup option for three items: total number of days, number of weekends and number of week days i.e. business days.

getDaysDifference(startDate As date, endDate As date, sourceDates As String, Type As String) 'sourceDates is if you provide the dates tab-delimited

It is available at http://www.holymackerelsoftware.com/CleanHaven/. If anyone wants the method, then contact me at holymackerel@mac.com