Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I found little code on the web about calculating working days in the week between two days, and I am stuck a little with holidays. basically I want to calculate woking days between two dates but excluding holidays also. so I tried with list of holiday days, but I dont know how to populate list programatically because I have table in sql server in which I saved dates of holidays, and I want to pull them out to that list or array

here is the code

public static List<DateTime> GetHolidays()
        {
            List<DateTime> holidays = new List<DateTime>();
            holidays.Add(new DateTime(2013, 1, 1));
            holidays.Add(new DateTime(2013, 1, 2));
            return holidays;
        }
private void btnSnimi_Click(object sender, EventArgs e)
        {
            if (listdani.SelectedIndex == 0)
            {
                List<DateTime> holidays = GetHolidays();

                int count = 0;

                while (DateTime.Compare(month1.SelectionStart, month2.SelectionEnd) <= 0)
                {
                    if (month1.SelectionStart.DayOfWeek != DayOfWeek.Sunday && month1.SelectionStart.DayOfWeek != DayOfWeek.Saturday && (!holidays.Contains(month1.SelectionStart)))
                    count++;

                    month1.SelectionStart = month1.SelectionStart.AddDays(1);
                }

                kalkulacija.Text = count.ToString();
            }
        }


well this works because I have those two dates in the list holidays but I dont want it to put them in the code, because I want to save those dates in running time and then get holidays dates

any help most welcome
Posted
Updated 30-Jan-13 11:16am
v2
Comments
DinoRondelly 30-Jan-13 18:28pm    
Use a table and at run time query the table and pull out all the relevant holidays, then for future holidays you could just add them to your table
Sergey Alexandrovich Kryukov 30-Jan-13 22:41pm    
dgv?
First of all, you need to tag your UI library or application type.
—SA

1 solution

Hello

You may implement your GetHolidays() method like below -

C#
public static List<datetime> GetHolidays()
{
     List<datetime> holidays = new List<datetime>();
     // write query to fetch holidays in string variable
     // execute in datareader
     // loop through datareader like
     while(reader.Read())
     {
         // here HOLIDAY_DATE is column name
         DateTime date = Convert.ToDateTime(reader["HOLIDAY_DATE"]);
         holidays.Add(date);
     }
     
     // close database connection and reader object

     return holidays;
}</datetime></datetime></datetime>


Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900