How to get the date of the next Sunday? [duplicate]

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

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).


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

/// <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/