-2

how to find whether the sunday falls between two dates.if sunday exist subtract one days from the difference between two days

3
  • 2
    Can you be a little bit more specific. Which Sunday, which dates? Dec 23, 2009 at 6:46
  • 2
    @Ranjana: You have asked 12 Questions, accepted none of the answers and never once voted on anything. The deal here is: You get answers, the user(s) providing the answer(s) get reputation.. please do your part. Thank You!
    – lexu
    Dec 23, 2009 at 6:49
  • 1
    -1 are you asking question or giving some sort of assignment?
    – PRR
    Dec 23, 2009 at 6:54

2 Answers 2

1

If I understand correctly, you are looking for something like this:

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;
}

The endDate needs to be the most recent date. The first part simply keeps us from checking if the start and end dates are more than 6 days apart (it will include a sunday, so no need to continue). The second bit just walks backward one day at a time from the endDate checking if Sunday is in there.

Once you know if sunday is part of the span, you can make whatever changes to the dates you want from the calling code.

0

Just because I like to be clever, I wrote it up this way:

public static int DaysExcludingSundays(DateTime start, DateTime end)
{
 return ((end - start).Days + 1) - ((((end - start).Days + 1) + (((int)start.DayOfWeek + 6) % 7)) / 7);
}

Feel free to copy and paste this code without understanding what it means. I enjoyed the puzzle.

Broken down:

int startOffset = ((int) start.DayOfWeek + 6) % 7;  
int totalInclusiveDays = (end - start).Days + 1;  
int numberOfSundays = (totalInclusiveDays + startOffset) / 7;  
int numberOfDaysWithoutSundays = totalInclusiveDays - numberOfSundays; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.