LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution
Highlighted

Calculating number of work days / business days

Hello Everyone,

Trying to calculate the number of business days (in LabVIEW) between two given dates but haven't found a library or tool to do this yet.

Basically the following formula:

____________________________

CalcBusinessDays(Date1 as integer, Date 2 as integer)

ReturnVal = Date 2 - Date 1 - (# of weekends contained) - (# of holidays contained)

Return ReturnVal

__________________________________________

Does anyone know of a tool that accomplishes this goal?  Done some quick searches on Ni.com, LAVA, google, VIPM, Open G and haven't found anything.  This has to exist and I've just missed it somehow.

Thanks,

Tim

Message 1 of 8
(874 Views)

Re: Calculating number of work days / business days

Well, aside from the Holidays question (which, I suppose, could be a simple Lookup, except that "holiday" is a somewhat vague term), it largely boils down to the following:

• How many days are between Date 1 and Date 2?
• How many of those days are Saturday or Sunday?

Both questions are (or should be) easy to answer (using Pencil and Paper -- think about it, pretend you don't have LabVIEW).  [Hint -- "Thirty Days Hath September, ..."].  It can get a little complicated if you are doing multi-year, and if you worry about Leap Year, of course.

Have you looked at the Time functions that come with LabVIEW?  Some of them do very interesting things with Dates that might make the problem even simpler.  But trust me, putting a few minutes into doing it as suggested in the previous paragraph will go a long way to your better understanding how to use the LabVIEW functions.

Bob Schor

Message 2 of 8
(854 Views)

Re: Calculating number of work days / business days

I agree with Bob (that does happen a lot!).

Message 3 of 8
(805 Views)
Solution
Accepted by topic author Tim_McClung

Re: Calculating number of work days / business days

In case anyone is looking: here is the weekend removal part of the equation.   If you wanted to add holiday removal in you could do that with a lookup table.  We decided to no go this route because there was little value in maintaining the table.  Hope this helps anyone looking.  There's tons of solutions for this in other programming languages, hopefully having it out there in labview will be useful as well.

This VI doesn't really care what day you start or end, it will always remove weekends.  If you want inclusive or exclusive start/end date this can be controlled by if you roll forward or backward on the movetomidnight snippet.

Best wishes to all and let me know if you have any questions,

Tim

CLA

Message 4 of 8
(765 Views)

Re: Calculating number of work days / business days

@Tim_McClung wrote:

In case anyone is looking: here is the weekend removal part of the equation.   If you wanted to add holiday removal in you could do that with a lookup table.  We decided to no go this route because there was little value in maintaining the table.  Hope this helps anyone looking.  There's tons of solutions for this in other programming languages, hopefully having it out there in labview will be useful as well.

This VI doesn't really care what day you start or end, it will always remove weekends.  If you want inclusive or exclusive start/end date this can be controlled by if you roll forward or backward on the movetomidnight snippet.

Best wishes to all and let me know if you have any questions,

Tim

CLA

There are two days in the year when there is more or less than 24 hours in the day. What that means is if your code is running at the WRONG time (the witching hour 2:00AM) the constant you are adding to fudge a day earlier will fail. On one day your constant of 86400 will land you back on the same day and on the other day you will skip over a day completely.

Spoiler
While I like a lot of Ben Franklin's work, when it comes to daylight savings... it was bad idea.

Now just coding in a some constants to see which day the clocks skips forward and back get complicated as well be the government can change the rules anytime they want...

Spoiler
("those that seek to change times and ways").

The Windows OS had to be upgraded the last time congress changed the rules. So what am I getting at? If you really need to be sure, you basically have to test each hour between your start and stop dates and rely on the OS to return the correct time.

Now if you are just trying to get an estimate and you do not have to exact, then ignore all of my hair-splitting.

Ben

Message 5 of 8
(749 Views)

Re: Calculating number of work days / business days

Ben,

I didn't need that level of specificity but you are correct that the problem of calculating time can become extremely complicated. As with most things the more accuracy you need the more time it's going to take.  I tried to create a solution that would work for most situations and was pliable enough that it could be modded to grow later.  It's not perfect but it does solve the problem and it gets someone who doesn't know how to do it off the ground (a lot of what I was going for by posting it out here).  Everyone has different needs with time management and calculation. I hope this is a handy tool to start from.

Tim

Message 6 of 8
(737 Views)

Re: Calculating number of work days / business days

@Tim_McClung wrote:

Ben,

...

Tim

You are welcome!

It is the 2:00AM hour when things break down. As long as you are outside that hour you may be OK.

How I found that out the hard way...

Spoiler

I had an application that was supposed to write a report everyday at the same time with the time be configured by the end user. Worked fine until the user tried out 2:00AM. On the day daylight saving time changed, the report did not get written! It took some playing with the system time and testing before I figured that one out.

Now for fun the next problem was checking when the report was written. Looking at the date the report file was modified using Explorer, there was an odd observation. The files that had been written yesterday at 2:00 now showed as being written at 1:00. It turns out that Windows reports the time in the current setting of daylight vs normal time.

Time is complicated enough without mucking with it.

Ben

Message 7 of 8
(722 Views)

Re: Calculating number of work days / business days

And don't even get started about GPS\Galileo\GNSS timing. Leap seconds and such. And of course those satellites have low gravity and high speed, so time goes faster (or slower?). It's mind blowing... Ever make a GNSS satellite simulator? You'll know what I mean.

Message 8 of 8
(704 Views)