How to get the number of working days per month (except the legal holidays for each country)?This is a question that i had to answer for the timing app. Here are some great resources on
DateTime class and also on
System.Globalization.
And here is my implementation on how to get this number:
private void nrZileLucratoare(int luna)
{
string numeLuna = "";
switch (luna)
{
case 1: numeLuna = "Jan"; break;
case 2: numeLuna = "Feb"; break;
case 3: numeLuna = "Mar"; break;
case 4: numeLuna = "Apr"; break;
case 5: numeLuna = "May"; break;
case 6: numeLuna = "Jun"; break;
case 7: numeLuna = "Jul"; break;
case 8: numeLuna = "Aug"; break;
case 9: numeLuna = "Sep"; break;
case 10: numeLuna = "Oct"; break;
case 11: numeLuna = "Nov"; break;
case 12: numeLuna = "Dec"; break;
}
int totalZile = DateTime.DaysInMonth(DateTime.Now.Year, luna);
int contor=0;
for (int i = 1; i <= totalZile; i++)
{
string Duminica = "Sun "+i+" "+numeLuna+" 9:30 AM 2009";
string Sambata = "Sat "+i+" "+numeLuna+" 9:30 AM 2009";
string format = "ddd d MMM h:mm tt yyyy";
DateTime dateTime;
if (DateTime.TryParseExact(Duminica, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
contor += 1;
}
if (DateTime.TryParseExact(Sambata, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
contor += 1;
}
}
MessageBox.Show((totalZile - contor).ToString()); // this is the actual number
}