Skip to content

Instantly share code, notes, and snippets.

@gleox
Last active December 27, 2018 09:09
Show Gist options
  • Save gleox/26bb25a587989b9b23a23c009ca445a0 to your computer and use it in GitHub Desktop.
Save gleox/26bb25a587989b9b23a23c009ca445a0 to your computer and use it in GitHub Desktop.
How to find the third Wednesday of the month of the specific time
using System;
// see: https://stackoverflow.com/questions/5421972/how-to-find-the-3rd-friday-in-a-month-with-c
namespace Demo
{
public class ScheduleTimeService
{
/// <summary>
/// How to find the third Wednesday of the month of the specific time
/// </summary>
/// <param name="time">the specific time</param>
/// <returns>return the date of the third Wednesday of the month</returns>
public DateTime GetThirdWednesday(DateTime time)
{
var firstDayOfMonth = new DateTime(time.Year, time.Month, 1);
var daysToNeed = (int)DayOfWeek.Wednesday - (int)firstDayOfMonth.DayOfWeek;
if (daysToNeed < 0)
{
daysToNeed = daysToNeed + 7;
}
daysToNeed = daysToNeed + 7 * 2;
var result = firstDayOfMonth.AddDays(daysToNeed);
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment