Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Every one,
How to calculate no of workingdays in month excluding Sundays ?


thanks & Regards

Hari
Posted
Comments
Sergey Alexandrovich Kryukov 9-Nov-11 0:56am    
Tried anything?
--SA

I think this will help you

C#
int year = 2011;
int month = 11;
int daysInMonth = 0;
int days = DateTime.DaysInMonth(year, month);
for (int i = 1; i <= days; i++)
{
    DateTime day = new DateTime(year, month, i);
    if (day.DayOfWeek != DayOfWeek.Sunday)
    {
        daysInMonth++;
    }
}


to calculate for every month in a year try this

C#
int year = 2011;
int monthsInYear = 12;
List<int> workDaysEveryMonth = new List<int>();
for (int month = 1; month <= monthsInYear; month++)
{
    int daysInMonth = 0;
    int days = DateTime.DaysInMonth(year, month);

    for (int day = 1; day <= days; day++)
    {
        DateTime currentDay = new DateTime(year, month, day);
        if (currentDay.DayOfWeek != DayOfWeek.Sunday)
        {
            daysInMonth++;
        }
    }

    workDaysEveryMonth.Add(daysInMonth);
}


you could then display the results using something like this e.g.

C#
string[] months = { "January", "Feburary", "March", "April", "May", "June",
                    "July", "August", "September", "October","November", "December" };
DateTime today = DateTime.Now;
Label1.Text = "There are " + workDaysEveryMonth[today.Month - 1] + " work days in " + months[today.Month - 1];
 
Share this answer
 
v10
Comments
Hari Krishna Prasad Inakoti 9-Nov-11 0:46am    
Thanks dude.But i want every month working days automatically.How can i write that logic plz help me.
LanFanNinja 9-Nov-11 0:57am    
I am not exactly sure what you are asking for but recheck my solution I have added some code that might help with this.
LanFanNinja 9-Nov-11 1:54am    
I have for you now my final version (version 10!!) I hope some of this will help you out.
Hari Krishna Prasad Inakoti 13-Nov-11 23:22pm    
i got this error workDaysEveryMonth.
The name 'workDaysEveryMonth' does not exist in the current context
LanFanNinja 14-Nov-11 1:58am    
workDaysEveryMonth is a List<T> declared in my solution as
List<int> workDaysEveryMonth = new List<int>();
I think you may be trying to access it outside of its scope try declaring workDaysEveryMonth as a class level variable.
 
Share this answer
 
Comments
Hari Krishna Prasad Inakoti 9-Nov-11 0:36am    
I want No of Working Days in month excluding sundays not in between 2days.

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