to calculate no of working days in a current month excluding sunday in windows application in .net
开发者_如何学Goto calculate no of working days in a current month excluding sunday in windows application in .net.please anyone do the helpfull as soon as possible..
This will compute the number on non-Sunday's in a month (example shows current month).
var daysThisMonthThatAreNotSundays =
Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)).Where(
d => new DateTime(DateTime.Now.Year, DateTime.Now.Month, d).DayOfWeek != DayOfWeek.Sunday).Count();
The idea is to exclude non-working days. Here's a basic example:
// define non working days of week
var nonWorkingDaysOfWeek = new List<DayOfWeek>
{
DayOfWeek.Sunday // hard-coded for example
};
// define specific non-working dates
var holidays = new List<DateTime>
{
new DateTime(2010, 12, 25) // hard-coded for example
};
// tally the working days
var currentYear = 2010; // hard-coded for example
var currentMonth = 12; // hard-coded for example
var daysInCurrentMonth = DateTime.DaysInMonth(currentYear, currentMonth);
var numberOfWorkingDays = 0;
for (var day = 1; day <= daysInCurrentMonth; day++)
{
var date = new DateTime(currentYear, currentMonth, day);
if (!nonWorkingDaysOfWeek.Contains(date.DayOfWeek) && !holidays.Contains(date))
{
numberOfWorkingDays++;
}
}
精彩评论