Last active
September 8, 2016 14:42
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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