All date related solution
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetnextSunday();
for (int i = 1; i <= 31; i++)
{
DateTime start = new DateTime(2015, 1, i);
DateTime stop = new DateTime(2015, 1, 31);
int totalWorkingDays = GetNumberOfWorkingDays(start, stop);
Console.WriteLine("There are {1} working days from Oct {0}, 2014 to Oct 31, 2014.", i, totalWorkingDays);
}
DateTime start1 = new DateTime(2015, 1, 1);
DateTime stop1 = new DateTime(2015, 1, 31);
int cntcoun = CountDays(start1, stop1);
GetAllSundays(start1, stop1);
}
}
public void GetnextSunday()
{
var date = DateTime.Now;
var nextSunday1 = date.AddDays(7 - (int)date.DayOfWeek);
DateTime d = DateTime.Today;
int offset = d.DayOfWeek - DayOfWeek.Monday;
DateTime lastMonday = d.AddDays(-offset);
DateTime nextSunday = lastMonday.AddDays(6);
}
private static int GetNumberOfWorkingDays(DateTime start, DateTime stop)
{
TimeSpan interval = stop - start;
int totalWeek = interval.Days / 7;
int totalWorkingDays = 5 * totalWeek;
int remainingDays = interval.Days % 7;
for (int i = 1; i <= remainingDays; i++)
{
DayOfWeek test = (DayOfWeek)(((int)start.DayOfWeek) % 7);
if (test >= DayOfWeek.Monday && test <= DayOfWeek.Friday)
totalWorkingDays++;
}
return totalWorkingDays;
}
public int CountDays(DateTime fromDate, DateTime toDate)
{
int noOfDays = 0;
DateTime fDate = Convert.ToDateTime(fromDate);
DateTime tDate = Convert.ToDateTime(toDate);
while (DateTime.Compare(fDate, tDate) <= 0)
{
if (fDate.DayOfWeek != DayOfWeek.Saturday && fDate.DayOfWeek != DayOfWeek.Sunday)
{
noOfDays += 1;
}
fDate = fDate.AddDays(1);
}
return noOfDays;
}
public void GetAllSundays(DateTime Date1, DateTime Date2)
{
TimeSpan DateDiff = Date2.Subtract(Date1);
List<DateTime> lstdate = new List<DateTime>();
for (int i = 0; i <= DateDiff.Days; i++)
{
if (Date1.Date.AddDays(i).DayOfWeek == DayOfWeek.Sunday)
{
lstdate.Add(Date1.Date.AddDays(i));
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetnextSunday();
for (int i = 1; i <= 31; i++)
{
DateTime start = new DateTime(2015, 1, i);
DateTime stop = new DateTime(2015, 1, 31);
int totalWorkingDays = GetNumberOfWorkingDays(start, stop);
Console.WriteLine("There are {1} working days from Oct {0}, 2014 to Oct 31, 2014.", i, totalWorkingDays);
}
DateTime start1 = new DateTime(2015, 1, 1);
DateTime stop1 = new DateTime(2015, 1, 31);
int cntcoun = CountDays(start1, stop1);
GetAllSundays(start1, stop1);
}
}
public void GetnextSunday()
{
var date = DateTime.Now;
var nextSunday1 = date.AddDays(7 - (int)date.DayOfWeek);
DateTime d = DateTime.Today;
int offset = d.DayOfWeek - DayOfWeek.Monday;
DateTime lastMonday = d.AddDays(-offset);
DateTime nextSunday = lastMonday.AddDays(6);
}
private static int GetNumberOfWorkingDays(DateTime start, DateTime stop)
{
TimeSpan interval = stop - start;
int totalWeek = interval.Days / 7;
int totalWorkingDays = 5 * totalWeek;
int remainingDays = interval.Days % 7;
for (int i = 1; i <= remainingDays; i++)
{
DayOfWeek test = (DayOfWeek)(((int)start.DayOfWeek) % 7);
if (test >= DayOfWeek.Monday && test <= DayOfWeek.Friday)
totalWorkingDays++;
}
return totalWorkingDays;
}
public int CountDays(DateTime fromDate, DateTime toDate)
{
int noOfDays = 0;
DateTime fDate = Convert.ToDateTime(fromDate);
DateTime tDate = Convert.ToDateTime(toDate);
while (DateTime.Compare(fDate, tDate) <= 0)
{
if (fDate.DayOfWeek != DayOfWeek.Saturday && fDate.DayOfWeek != DayOfWeek.Sunday)
{
noOfDays += 1;
}
fDate = fDate.AddDays(1);
}
return noOfDays;
}
public void GetAllSundays(DateTime Date1, DateTime Date2)
{
TimeSpan DateDiff = Date2.Subtract(Date1);
List<DateTime> lstdate = new List<DateTime>();
for (int i = 0; i <= DateDiff.Days; i++)
{
if (Date1.Date.AddDays(i).DayOfWeek == DayOfWeek.Sunday)
{
lstdate.Add(Date1.Date.AddDays(i));
}
}
}