Click here to Skip to main content
14,643,812 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All,

I want to calculate no of Sunday between two dates.

Please let me know how to calculate it?

Thanking you
Mohammad wasif
Posted
Updated 27-May-20 6:32am
Rate this:
Please Sign up or sign in to vote.

Solution 2

See the code below, provided that sDate < fDate.
Add conditions to manipulate the sDate(startDate) and fDate(finishDate)

//DateTime fdate = DateTime.Now.AddDays(10);
DateTime fdate = yourFinishDate;
//DateTime sdate = DateTime.Now.AddDays(-1);
DateTime sdate = yourStartDate;
TimeSpan ts = fdate - sdate;
var sundays = ((ts.TotalDays / 7) + (sdate.DayOfWeek == DayOfWeek.Sunday || fdate.DayOfWeek == DayOfWeek.Sunday || fdate.DayOfWeek > sdate.DayOfWeek ? 1 : 0));

sundays = Math.Round(sundays - .5, MidpointRounding.AwayFromZero);


Btw, code in c#. Good luck!
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

DateTime fdate = yourFinishDate;
DateTime sdate = yourStartDate;

int CountSundays = (1 + fdate.Subtract(sdate).Days + (6 + (int)sdate.DayOfWeek) % 7) / 7;
   
Comments
CHill60 17-Sep-15 7:10am
   
Apart from the fact this question was answered over 4 years ago, if I create a simple winform and run this code with fdate = DateTime.Now and sdate = new DateTime(2015,1,1) I get a result of 25 instead of 37.
I get the correct answer with solution 1
Malcolm J 17-Sep-15 11:17am
   
I get 37 with my solution. Perhaps you typed it incorrectly.

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime sdate = new DateTime(2015, 1, 1);
DateTime fdate = DateTime.Now;

DateTime ptrdate = sdate;
int count = 0;
while (ptrdate <= fdate)
{
if (ptrdate.DayOfWeek == DayOfWeek.Sunday) count++;
ptrdate = ptrdate.AddDays(1);
}

int mycount = (1 + fdate.Subtract(sdate).Days + (6 + (int)sdate.DayOfWeek) % 7) / 7;
Console.WriteLine("{0} = {1}", count, mycount);
Console.ReadLine();
}
}
}
CHill60 17-Sep-15 12:30pm
   
Bizarre. I cut & paste it into another new project and now it works. Must have been an issue in the scratch solution I'd been using ... now to find out what the problem was at my end.
Sincere apologies
Malcolm J 17-Sep-15 13:03pm
   
No worries. :)
Rate this:
Please Sign up or sign in to vote.

Solution 4

static int CountDays(DayOfWeek day, DateTime start, DateTime end)
   {
       TimeSpan ts = end - start;                       // Total duration
       int count = (int)Math.Floor(ts.TotalDays / 7);   // Number of whole weeks
       int remainder = (int)(ts.TotalDays % 7);         // Number of remaining days
       int sinceLastDay = (int)(end.DayOfWeek - day);   // Number of days since last [day]
       if (sinceLastDay < 0) sinceLastDay += 7;         // Adjust for negative days since last [day]

       // If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.
       if (remainder >= sinceLastDay) count++;

       return count;
   }

[source]

-KR
   
Comments
CHill60 17-Sep-15 7:12am
   
Apart from the fact this question was answered over 4 years ago, if I create a simple winform and run this code with fdate = DateTime.Now and sdate = new DateTime(2015,1,1) I get a result of 25 instead of 37.
I get the correct answer with solution 1


Ignore my comment - now works in a brand new solution. Don't know what was going on earlier but clearly a problem at my end.
Sincere apologies
Rate this:
Please Sign up or sign in to vote.

Solution 5

Try this logic once...Hope it helps you

DateTime startDate = new DateTime(2015, 8, 31);
            DateTime endDate = DateTime.Now;
            var day=endDate.Day;
            TimeSpan diff = endDate - startDate;
            //int days = diff.Days;
            int count = 0;
            for (var i = 0; i < day; i++)
            {
                DateTime d = new DateTime(endDate.Year, endDate.Month, i + 1);
                if (d.DayOfWeek == DayOfWeek.Sunday) {
                    count = count + 1;
                }
            }
            Console.WriteLine(count);


Thanks..
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100