Solved

How to calculate NETWORKDAYS in C#

Posted on 2009-07-01
3
1,799 Views
Last Modified: 2012-05-07
I use this project mainly as a learning exercise for DataSet and LINQ.

The background is that I have a report in Excel html format that I read into a .NET 3.5 C# Windows Forms Application. I then display the data in a DataGridView and I also want to calculate some statistics on the data. To do so, I need to add a new column showing the elapsed number of working days between two dates.

In my DataSet, I have two columns, "Open Date" and "Close Date". In Excel I was able to use a formula like "=NETWORDAYS(B7:C7, Z2:Z50) to calculate the number of working days elapsed between the two dates.

Is there an easy way of achieving the same thing in my C# code, adding a new column to my DataTable?

The code needs a bit more polishing, eg. adding some error trapping etc but it shows what I have at the moment.
private void GetDataFromExcel(string fileName)
		{
			String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + fileName + ";Extended Properties=HTML Import;";
 
			OleDbConnection dbConnection = new OleDbConnection(connectionString);
			dbConnection.Open();
 
			string select = @"SELECT * FROM [Table]";
 
			OleDbCommand selectCommand = new OleDbCommand(select, dbConnection);
 
			OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter();
 
			dbDataAdapter.SelectCommand = selectCommand;
 
			DataSet ds = new DataSet();
 
			dbDataAdapter.Fill(ds, "XLData");
 
			mainDataGridView.DataSource = ds.Tables[0];
 
			dbConnection.Close();
 
		}

Open in new window

0
Comment
Question by:Sharp2b
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 24759034
There's nothing built-in, but here's a method that calculates workdays between two dates and allows you to pass in a list of excluded dates (e.g. holidays).
private int CountWorkDays( DateTime startDate, DateTime endDate, List<DateTime> excludedDates )
{
    int dayCount = 0;
    int inc = 1;
    bool endDateIsInPast = startDate > endDate;
    DateTime tmpDate = startDate;
    DateTime finiDate = endDate;
 
    if( endDateIsInPast )
    {
        // Swap dates around
        tmpDate = endDate;
        finiDate = startDate;
 
        // Set increment value to -1, so it DayCount decrements rather 
        // than increments
        inc = -1;
    }
 
    while( tmpDate <= finiDate )
    {
        if( !excludedDates.Contains( tmpDate ) )
        {
            dayCount += inc;
        }
 
        // Move onto next day
        tmpDate = tmpDate.AddDays( 1 );
    }
 
    return dayCount;
}

Open in new window

0
 

Author Comment

by:Sharp2b
ID: 24760883
What a great start of the day!

Thank you so much!

While getting ready to go to work this morning, I was thinking about how to implement a function like this as I figured there was no pre-built function. My thoughts was in line with this but one thing I was not immediately aware of was the Contains() method. I would probably have found it eventually but now you probably saved me at least 1/2 day of work. Since this is more of a "hobby" project, I can't spend too much time on it, at least not during the day. ;-) But now there is a good chance that I will be able to finish it off.
0
 

Author Closing Comment

by:Sharp2b
ID: 31598697
Exactly what I was looking for. Nice, short and easy to follow. It was also a good example to learn from as I still see myself as a beginner in this area. Hence it's also valuable to me for future projects.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question