Skip to content

Instantly share code, notes, and snippets.

@JaimeStill
Created December 3, 2019 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaimeStill/8c072357049444439d52b309873cebbe to your computer and use it in GitHub Desktop.
Save JaimeStill/8c072357049444439d52b309873cebbe to your computer and use it in GitHub Desktop.
In C#, project a date in the future and ensure it does not fall on the weekend
using System;
public static class DateExtensions
{
static DateTime PreventWeekend(this DateTime dateTime)
{
switch (dateTime.DayOfWeek)
{
case DayOfWeek.Saturday:
return dateTime.AddDays(2);
case DayOfWeek.Sunday:
return dateTime.AddDays(1);
default:
return dateTime;
}
}
// Example Usage
static DateTime Calculate24Hour(this Datetime dateTime) => dateTime.AddHours(20).PreventWeekend();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment