How to find the 3rd Friday in a month with C#?
I haven't tested this, but since the third Friday can't possibly occur before the 15th of the month, create a new DateTime, then just increment until you get to a Friday.
DateTime thirdFriday= new DateTime(yourDate.Year, yourDate.Month, 15);
while (thirdFriday.DayOfWeek != DayOfWeek.Friday)
{
thirdFriday = thirdFriday.AddDays(1);
}
How can I determine if the first or third Wednesday of the month has yet to arrive, and if so, which one is nearest to the current date?
You could also try it like this:
protected DateTime getFirstWednesdayOfMonth(DateTime seedDate)
{
DateTime wed1 = new DateTime(seedDate.Year, seedDate.Month, 1); //1st Wednesday can start on the 1st of the month
while (wed1.DayOfWeek != DayOfWeek.Wednesday)
{
wed1 = wed1.AddDays(1);
}
return wed1;
}
protected DateTime getThirdWednesdayOfMonth(DateTime seedDate)
{
DateTime wed3 = new DateTime(seedDate.Year, seedDate.Month, 15); //3rd Wednesday cannot start prior to the 15th of the month
while (wed3.DayOfWeek != DayOfWeek.Wednesday)
{
wed3 = wed3.AddDays(1);
}
return wed3;
}
protected void Button1_Click(object sender, EventArgs e)
{
DateTime Now = DateTime.Today;
DateTime wed1 = getFirstWednesdayOfMonth(Now);
DateTime wed3 = getThirdWednesdayOfMonth(Now);
if (Now < wed1)
{
lblDate.Text = wed1.ToString();
}
else if (Now < wed3)
{
lblDate.Text = wed3.ToString();
}
}
How to calculate 2nd Friday of Month in C#
Slight variation on @druttka: using an extension method.
public static DateTime NthOf(this DateTime CurDate, int Occurrence , DayOfWeek Day)
{
var fday = new DateTime(CurDate.Year, CurDate.Month, 1);
var fOc = fday.DayOfWeek == Day ? fday : fday.AddDays(Day - fday.DayOfWeek);
// CurDate = 2011.10.1 Occurance = 1, Day = Friday >> 2011.09.30 FIX.
if (fOc.Month < CurDate.Month) Occurrence = Occurrence+1;
return fOc.AddDays(7 * (Occurrence - 1));
}
Then called it like this:
for (int i = 1; i < 13; i++)
{
Console.WriteLine(new DateTime(2011, i,1).NthOf(2, DayOfWeek.Friday));
}
How to get last Friday of month(s) using .NET
You already have the list of Fridays in the given range. Now just query this again like this:
List<DateTime> lastFridays = (from day in fridays
where day.AddDays(7).Month != day.Month
select day).ToList<DateTime>();
Hope this helps.
Calculate the Week day of months from given date?
Just add 4 weeks to input date, add 7 more days if necessary:
static DateTime GetNextDate(DateTime d1)
{
System.Diagnostics.Debug.Assert(d1.Day <= 28, "Behavior not described");
DateTime d2 = d1.AddDays(28);
// the following evaluates to 1 for 1-7, 2 for 8-14, etc
int n1 = (d1.Day - 1) / 7 + 1;
int n2 = (d2.Day - 1) / 7 + 1;
if (n2 != n1)
{
d2 = d2.AddDays(7);
}
return d2;
}
Sample input and output:
Thu 2018-Mar-01 > Thu 2018-Apr-05
Fri 2018-Mar-02 > Fri 2018-Apr-06
Sat 2018-Mar-03 > Sat 2018-Apr-07
Sun 2018-Mar-04 > Sun 2018-Apr-01
Mon 2018-Mar-05 > Mon 2018-Apr-02
Tue 2018-Mar-06 > Tue 2018-Apr-03
Wed 2018-Mar-07 > Wed 2018-Apr-04
Thu 2018-Mar-08 > Thu 2018-Apr-12
Fri 2018-Mar-09 > Fri 2018-Apr-13
Sat 2018-Mar-10 > Sat 2018-Apr-14
Sun 2018-Mar-11 > Sun 2018-Apr-08
Mon 2018-Mar-12 > Mon 2018-Apr-09
Tue 2018-Mar-13 > Tue 2018-Apr-10
Wed 2018-Mar-14 > Wed 2018-Apr-11
Thu 2018-Mar-15 > Thu 2018-Apr-19
Fri 2018-Mar-16 > Fri 2018-Apr-20
Sat 2018-Mar-17 > Sat 2018-Apr-21
Sun 2018-Mar-18 > Sun 2018-Apr-15
Mon 2018-Mar-19 > Mon 2018-Apr-16
Tue 2018-Mar-20 > Tue 2018-Apr-17
Wed 2018-Mar-21 > Wed 2018-Apr-18
Thu 2018-Mar-22 > Thu 2018-Apr-26
Fri 2018-Mar-23 > Fri 2018-Apr-27
Sat 2018-Mar-24 > Sat 2018-Apr-28
Sun 2018-Mar-25 > Sun 2018-Apr-22
Mon 2018-Mar-26 > Mon 2018-Apr-23
Tue 2018-Mar-27 > Tue 2018-Apr-24
Wed 2018-Mar-28 > Wed 2018-Apr-25
C#: Finding Out Which Monday Is The 3rd Monday Of The Month?
public bool IsThirdMondayOfMonth(DateTime dt)
{
if(dt.DayOfWeek == DayOfWeek.Monday && dt.Day > 14 && dt.Day <= 21)
{
return true;
}
return false;
}
Calculate the last DayOfWeek of a month
Can't find a handy one-liner but this one works:
static DateTime LastDayOfWeekInMonth(DateTime day, DayOfWeek dow)
{
DateTime lastDay = new DateTime(day.Year, day.Month, 1).AddMonths(1).AddDays(-1);
DayOfWeek lastDow = lastDay.DayOfWeek;
int diff = dow - lastDow;
if (diff > 0) diff -= 7;
System.Diagnostics.Debug.Assert(diff <= 0);
return lastDay.AddDays(diff);
}
Find third Sunday of each month occur between given two dates
This should do the trick:
public List<DateTime> ThirdSundayOfEachMonth( DateTime startdate, DateTime enddate )
{
List<DateTime> result = new List<DateTime>();
int sundaymonthcount = 0;
for( DateTime traverser = new DateTime(startdate.Year, startdate.Month, 1); traverser <= enddate; traverser = traverser.AddDays(1) ){
if( traverser.DayOfWeek == DayOfWeek.Sunday ) sundaymonthcount++;
if( sundaymonthcount == 3 && traverser > startdate ){
result.Add(traverser);
sundaymonthcount = 0;
traverser = new DateTime( traverser.Year, traverser.Month, 1 ).AddMonths(1);
}
}
return result;
}
Related Topics
Why Datetime.Addhours Doesn't Seem to Work
How to Read Regex Captures in C#
Should I Always Return Ienumerable<T> Instead of Ilist<T>
C# Entity-Framework: How to Combine a .Find and .Include on a Model Object
How to Create an Instance of an Arbitrary Array Type at Runtime
Trying to Insert Datetime.Now into Date/Time Field Gives "Data Type Mismatch" Error
How to Access Openxml Content by Page Number
Lambda Variable Capture in Loop - What Happens Here
Getting Overlapping Regex Matches in C#
How to Do Generic Polymorphism on Open Types in C#
Practical Uses for the "Internal" Keyword in C#
How Does Startcoroutine/Yield Return Pattern Really Work in Unity
Windows Forms Designer and Wpf Designer for .Net Core