ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

NextDayOfWeek

Will return the next occurring day of week

Source

public static DateTime NextDayOfWeek(this DateTime dt, DayOfWeek day)
{
	var d = new GregorianCalendar().AddDays(dt, -((int)dt.DayOfWeek) + (int)day);
	return (d.Day < dt.Day) ? d.AddDays(7) : d;
}

Example

DateTime.Now.NextDayOfWeek(DayOfWeek.Tuesday)

Author: Tony Musico

Submitted on: 22 mrt. 2016

Language: C#

Type: System.DateTime

Views: 4700