ASP.NET - count no of sunday between two dates

Asked By ashok m on 13-Jun-11 02:06 AM
how to count no of sunday between two dates
Kirtan Patel replied to ashok m on 13-Jun-11 02:13 AM
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddDays(30);
 
int count = 0;
for (DateTime d = d1; d <= d2; d = d.Add(1))
{
  if (d.DayOfWeek == DayOfWeek.Sunday)
  {
    count++;
  }
}
Response.Write("total Sundays =" + count);
dipa ahuja replied to ashok m on 13-Jun-11 02:15 AM
Try this code:
void isSunday()
  {
    DateTime startDate = new DateTime(2011, 1, 1);
    DateTime endDate = new DateTime(2011, 1, 20);
 
    TimeSpan diff = endDate - startDate;
 
    int days = diff.Days;
    int countSundays = 0;
    for (var i = 0; i <= days; i++)
    {
      var testDate = startDate.AddDays(i);
      switch (testDate.DayOfWeek)
      {
        case DayOfWeek.Sunday:
          Response.Write("<br>sunday found on " + testDate.ToShortDateString());
          countSundays++;
          break;
      }
    }
    Response.Write("<Br>Total Sunday:" + countSundays);     
  }
Reena Jain replied to ashok m on 13-Jun-11 02:16 AM
hi,

try this code

int numberOfSaturdays;
int numberOfSundays;
DateTime startDate = new DateTime(2009, 01, 08);
DateTime endDate = new DateTime(2009, 01, 21);
GetWeekendDaysBetween(startDate, endDate, out numberOfSaturdays, out numberOfSundays);

hope this will help you
Reena Jain replied to ashok m on 13-Jun-11 02:17 AM
hi,

here is one more good code works for me

 private int CalculateWeekends(DateTime DateTime1, DateTime DateTime2)
 {
   int iReturn = 0;
   TimeSpan xTimeSpan;
   if (DateTime2 > DateTime1)
   xTimeSpan = DateTime2.Subtract(DateTime1);
   else
   xTimeSpan = DateTime1.Subtract(DateTime2);
   int iDays = 5 + System.Convert.ToInt32(xTimeSpan.TotalDays);
   iReturn = (iDays / 7);
   return iReturn;
 }

Hope this will help you
Anoop S replied to ashok m on 13-Jun-11 03:29 AM

From this link:

public static int Weekdays(DateTime dtmStart, DateTime dtmEnd)
{
  // This function includes the start and end date in the count if they fall on a weekday
  int dowStart = ((int)dtmStart.DayOfWeek == 0 ? 7 : (int)dtmStart.DayOfWeek);
  int dowEnd = ((int)dtmEnd.DayOfWeek == 0 ? 7 : (int)dtmEnd.DayOfWeek);
  TimeSpan tSpan = dtmEnd - dtmStart;
  if (dowStart <= dowEnd)
  {
    return (((tSpan.Days / 7) * 5) + Math.Max((Math.Min((dowEnd + 1), 6) - dowStart), 0));
  }
  return (((tSpan.Days / 7) * 5) + Math.Min((dowEnd + 6) - Math.Min(dowStart, 6), 5));
}

Jitendra Faye replied to ashok m on 13-Jun-11 04:33 AM

Try this function this will give sundays between given dates-

public static bool DoesIncludeSunday(DateTime startDate, DateTime endDate)
{
    bool r = false;
    TimeSpan testSpan = new TimeSpan(6, 0, 0, 0);
    TimeSpan actualSpan =endDate - startDate;

    if (actualSpan >= testSpan) { r = true; }
    else
    {
      DateTime checkDate = endDate;
      while (checkDate > startDate)
      {
        r = (checkDate.DayOfWeek == DayOfWeek.Sunday);
        if(r) { break; }
        checkDate = checkDate.AddDays(-1);
      }
    }

    return r;
}

try and let me know.