Click here to Skip to main content
11,923,046 members (60,875 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0

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)))
                    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 30-Jan-13 10:35am
Edited 30-Jan-13 12:16pm
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
First of all, you need to tag your UI library or application type.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


You may implement your GetHolidays() method like below -

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
         // here HOLIDAY_DATE is column name
         DateTime date = Convert.ToDateTime(reader["HOLIDAY_DATE"]);
     // close database connection and reader object

     return holidays;


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151125.3 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100