subracting sundays and get the remaining days of the particular month
Except sundays remaining days to be displayed textbox datetimepicker(calendar)
for calendar i use datetimepicker
in the textbox i want to display the days subracting the sundays and remaining days to be displayed in the textbox.
please send the code.
Rgds,
Narsiman P
Rahul BhattPosted Jul 10, 2012, 7:23 AM
Roy SPosted Jul 10, 2012, 4:52 AM
DateTime SDate = new DateTime(month: cal.Month, day: 1, year: cal.Year);
Rahul BhattPosted Jul 10, 2012, 2:22 AM
First you need to take First Date and last Date of selected Month
Using DateTime.DaysInMonth You can get no of Days in Month.
DateTime SDate = Convert.ToDateTime(calDate.Month.ToString() + "/01/" + calDate.Year.ToString());
int DaysInMonth = DateTime.DaysInMonth(cal.SelectedDate.Year, cal.SelectedDate.Month);
DateTime EDate = SDate.AddDays(DaysInMonth - 1);
Now Find No Of Sunday
Int32 SunCnt = 0;
while (SDate.DayOfWeek != DayOfWeek.Sunday)
{
SDate = SDate.AddDays(1);
}
SunCnt = 1;
SDate = SDate.AddDays(7);
while (SDate <= EDate)
{
SunCnt += 1;
SDate = SDate.AddDays(7);
}
Now Exclude sunday from Total no of Days and get your final result
int FinalSalaryDay = DaysInMonth - SunCnt;
Full Source Code ::
private string GetDaysInMonthExceptSunday(DateTime cal)
{
DateTime SDate = Convert.ToDateTime( cal.Month.ToString() + "/01/"+ cal.Year.ToString());
int DaysInMonth = DateTime.DaysInMonth(cal.Year, cal.Month);
DateTime EDate = SDate.AddDays(DaysInMonth - 1);
Int32 SunCnt = 0;
while (SDate.DayOfWeek != DayOfWeek.Sunday)
{
SDate = SDate.AddDays(1);
}
SunCnt = 1;
SDate = SDate.AddDays(7);
while (SDate <= EDate)
{
SunCnt += 1;
SDate = SDate.AddDays(7);
}
int FinalSalaryDay = DaysInMonth - SunCnt;
return FinalSalaryDay.ToString();
}
Roy SPosted Jul 9, 2012, 2:11 PM
You could skip the first week (incomplete or not, will include the first sunday) and divide the rest of the days by 7 to get the amount of sundays. and subtract that number from the total amount of days in the month (not to forget the first sunday so subtract 1).
int GetDaysExlSunDay(int year, int month)
{
//create a data set to first day of that month of that year
var date = new DateTime(year: year, month: month, day: 1);
//get the amount of days in that month
int daysInMonth = DateTime.DaysInMonth(date.Year, date.Month);
//temporal variable to see how many days are in the first week
int tmp = 0;
//fast forward to first day after the first sunday
do
{
date = date.AddDays(1);
tmp++;
}
while (date.DayOfWeek != DayOfWeek.Monday);
//subtract amount of sundays and not to forget the first sunday
return daysInMonth - (daysInMonth - tmp) / 7 - 1;
}
Or a much easier to understand method (but perhaps a tiny bit slower). This method just keeps adding days and adding one to the result if it's not sunday. until the next month is reached.
int GetDaysExlSunDay(int year, int month)
{
//create a data set to first day of that month of that year
var date = new DateTime(year: year, month: month, day: 1);
int res = 0;
while (date.Month == month)
{
//if not sunday add one to res
if (date.DayOfWeek != DayOfWeek.Sunday)
{
res++;
}
//go to next day
date = date.AddDays(1);
}
return res;
}