Skip to content

Instantly share code, notes, and snippets.

@amogram
Last active September 8, 2016 14:42
Show Gist options
  • Save amogram/1b93298307a3cedad9b072e1d5218792 to your computer and use it in GitHub Desktop.
Save amogram/1b93298307a3cedad9b072e1d5218792 to your computer and use it in GitHub Desktop.
Gets the date for the next day. If it's today, return today.
using System;
namespace Common.DateTimeHelpers
{
public static class DateTimeHelpers
{
/// <summary>
/// Gets the date of the next specified day. If it's today, return today.
/// </summary>
/// <param name="from">The date</param>
/// <param name="dayOfWeek">Target day of week</param>
/// <returns>Date of next day</returns>
public static System.DateTime GetNextOrToday(this System.DateTime from, DayOfWeek dayOfWeek)
{
if (from.DayOfWeek == dayOfWeek)
{
return from;
}
var start = (int)from.DayOfWeek;
var target = (int)dayOfWeek;
if (target <= start)
{
target += 7;
}
return from.AddDays(target - start);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment