Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 30-Jan-13 9:35am
Edited 30-Jan-13 11:16am
v2
Comments
DinoRondelly at 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 at 30-Jan-13 22:41pm
   
dgv?
First of all, you need to tag your UI library or application type.
—SA

1 solution

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

Solution 1

Hello
 
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
     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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 CPallini 450
2 OriginalGriff 270
3 George Jonsson 256
4 Suvabrata Roy 229
0 OriginalGriff 4,873
1 CPallini 3,900
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,646
4 Gihan Liyanage 2,236


Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
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