How to get the date of the next Sunday?

C#.NetDatetime

C# Problem Overview


> Possible Duplicate:
> ASP.net get the next tuesday

Given a day of the month, how can I get the next sunday from that day?

So if I pass in Tuesday September 13th 2011, it will return Sept. 18th.

C# Solutions


Solution 1 - C#

I use this extension method:

public static DateTime Next(this DateTime from, DayOfWeek dayOfWeek)
{
    int start = (int)from.DayOfWeek;
    int target = (int)dayOfWeek;
    if (target <= start)
        target += 7;
    return from.AddDays(target - start);
}

Solution 2 - C#

date.AddDays(7 - (int)date.DayOfWeek) should do it I think.

date.DayOfWeek will return an enum value that represents the day (where 0 is Sunday).

Solution 3 - C#

var date = DateTime.Now;
var nextSunday = date.AddDays(7 - (int) date.DayOfWeek);    

If you need nearest sunday, code little bit different (as if you're on sunday, nearest sunday is today):

var nearestSunday = date.AddDays(7 - date.DayOfWeek == DayOfWeek.Sunday ? 7 : date.DayOfWeek);

Solution 4 - C#

/// <summary>
/// Finds the next date whose day of the week equals the specified day of the week.
/// </summary>
/// <param name="startDate">
/// The date to begin the search.
/// </param>
/// <param name="desiredDay">
/// The desired day of the week whose date will be returneed.
/// </param>
/// <returns>
/// The returned date is on the given day of this week.
/// If the given day is before given date, the date for the
/// following week's desired day is returned.
/// </returns>
public static DateTime GetNextDateForDay( DateTime startDate, DayOfWeek desiredDay )
{
    // (There has to be a better way to do this, perhaps mathematically.)
    // Traverse this week
    DateTime nextDate = startDate;
    while( nextDate.DayOfWeek != desiredDay )
        nextDate = nextDate.AddDays( 1D );

    return nextDate;
}

Source:

http://angstrey.com/index.php/2009/04/25/finding-the-next-date-for-day-of-week/

Solution 5 - C#

Here's the code:

int dayOfWeek = (int) DateTime.Now.DayOfWeek;
DateTime nextSunday = DateTime.Now.AddDays(7 - dayOfWeek).Date;

Get first the numerical value of the day of the week, in your example: Tuesday = 2

Then subtract it from Sunday, 7 -2 = 5 days to be added to get the next Sunday date. :)

Solution 6 - C#

DateTime dt=dateTime;
do {
  dt=dt.AddDays(1);
} while(dt.DayOfWeek!= DayOfWeek.Sunday);
// 'dt' is now the next Sunday

Your question isn't clear whether the same date is returned if the date is already a Sunday; I assume no, but change the above to a while loop if I'm wrong.

Solution 7 - C#

Example using recursion

    private static DateTime GetNextSunday(DateTime dt)
    {
        var tomorrow = dt.AddDays(1);
        if (tomorrow.DayOfWeek != DayOfWeek.Sunday)
        {
            GetNextSunday(tomorrow);
        }

        return tomorrow;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncodecompletingView Question on Stackoverflow
Solution 1 - C#Thomas LevesqueView Answer on Stackoverflow
Solution 2 - C#JoeyView Answer on Stackoverflow
Solution 3 - C#GiedriusView Answer on Stackoverflow
Solution 4 - C#Jason QuinnView Answer on Stackoverflow
Solution 5 - C#Bryan HongView Answer on Stackoverflow
Solution 6 - C#Peter O.View Answer on Stackoverflow
Solution 7 - C#Peter KellyView Answer on Stackoverflow