Solved

How to calculate NETWORKDAYS in C#

Posted on 2009-07-01
3
1,862 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 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