## How to find the 3rd Friday in a month with C#?

last friday of every month 2020

get nth weekday of month c#

3rd friday of the month options

last friday of month

get week number of month from date c#

get second monday of month c#

c# get second sunday of month

Given a date (of type `DateTime`

), how do I find the 3rd Friday in the month of that date?

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); }

**Get Third Friday of a month - C# (C sharp): Microsoft,** Input : Number of days in month = 28 First day = Wednesday Output The third one will be if the month has 30 days, then the day on which the� How to find the 3rd Friday in a month with C#? Hi everyone, I've wrote a little console utility that spits out a line into a text file. I want this line to include the second Friday of the current month. Is there any way to do this? Thanks everyone!

I'm going to repeat my answer from here with one little addition.

The language-agnostic version:

To get the first particular day of the month, start with the first day of the month: yyyy-mm-01. Use whatever function is available to give a number corresponding to the day of the week; in C# this would be `DateTime.DayOfWeek`

. Subtract that number from the day you are looking for; for example, if the first day of the month is Wednesday (3) and you're looking for Friday (5), subtract 3 from 5, leaving 2. If the answer is negative, add 7. Finally add that to the first of the month; for my example, the first Friday would be the 3rd.

To get the last Friday of the month, find the first Friday of the next month and subtract 7 days.

To get the 3rd Friday of the month, add 14 days to the first Friday.

**Find the number of times every day occurs in a month,** I need to read the calendar and put the date of the third Friday of each month 19 15:43 c.txt -rwxr-xr-x 1 user userg 1596 Mar 21 15:43 d.txt -rwxr-xr-x 1 user� 'changes the date supplied to the 15th, adds or subtracts num month(s) to the date, 'and finds the first Friday on or after the 15th, which must be the 3rd Friday of that month dt = dt.AddDays(15 - dt.Day) dt = dt.AddMonths(num) For x As Integer = 1 To 7 If dt.DayOfWeek = DayOfWeek.Friday Then Exit For End If dt = dt.AddDays(1) Next

I followed User:Mark Ransom's algorithm and wrote a generalized day finder. For example to get the 3rd friday of december 2013,

int thirdFriday = DayFinder.FindDay(2013, 12, DayOfWeek.Friday, 3);

And here is the function definition. It doesn't have any iterative loops, so its efficient.

public class DayFinder { //For example to find the day for 2nd Friday, February, 2016 //=>call FindDay(2016, 2, DayOfWeek.Friday, 2) public static int FindDay(int year, int month, DayOfWeek Day, int occurance) { if (occurance <= 0 || occurance > 5) throw new Exception("Occurance is invalid"); DateTime firstDayOfMonth = new DateTime(year, month, 1); //Substract first day of the month with the required day of the week var daysneeded = (int)Day - (int)firstDayOfMonth.DayOfWeek; //if it is less than zero we need to get the next week day (add 7 days) if (daysneeded < 0) daysneeded = daysneeded + 7; //DayOfWeek is zero index based; multiply by the Occurance to get the day var resultedDay = (daysneeded + 1) + (7 * (occurance - 1)); if (resultedDay > (firstDayOfMonth.AddMonths(1) - firstDayOfMonth).Days) throw new Exception(String.Format("No {0} occurance(s) of {1} in the required month", occurance, Day.ToString())); return resultedDay; } }

**how can i find the third friday of each month?,** Ok, I have data spanning numerous years, and part of my analysis requires me to know the Monday that precedes the third Friday of every month. Right now I am� Check what day of the week the first day of the next month is on, then subtract enough days to get a Friday. Or, if you already have a list of Fridays, return only those for which adding 7 days gives a date in the next month.

Probably best to abstract this to a method to do any date/day combination:

(Extension Method)

public static bool TryGetDayOfMonth(this DateTime instance, DayOfWeek dayOfWeek, int occurance, out DateTime dateOfMonth) { if (instance == null) { throw new ArgumentNullException("instance"); } if (occurance <= 0 || occurance > 5) { throw new ArgumentOutOfRangeException("occurance", "Occurance must be greater than zero and less than 6."); } bool result; dateOfMonth = new DateTime(); // Change to first day of the month DateTime dayOfMonth = instance.AddDays(1 - instance.Day); // Find first dayOfWeek of this month; if (dayOfMonth.DayOfWeek > dayOfWeek) { dayOfMonth = dayOfMonth.AddDays(7 - (int)dayOfMonth.DayOfWeek + (int)dayOfWeek); } else { dayOfMonth = dayOfMonth.AddDays((int)dayOfWeek - (int)dayOfMonth.DayOfWeek); } // add 7 days per occurance dayOfMonth = dayOfMonth.AddDays(7 * (occurance - 1)); // make sure this occurance is within the original month result = dayOfMonth.Month == instance.Month; if (result) { dateOfMonth = dayOfMonth; } return result; }

Results:

DateTime myDate = new DateTime(2013, 1, 1) DateTime dateOfMonth; myDate.TryGetDayOfMonth(DayOfWeek.Sunday, 1, out dateOfMonth) // returns: true; dateOfMonth = Sunday, 1/6/2013 myDate.TryGetDayOfMonth(DayOfWeek.Sunday, 4, out dateOfMonth) // returns: true; dateOfMonth = Sunday, 1/27/2013 myDate.TryGetDayOfMonth(DayOfWeek.Sunday, 5, out dateOfMonth) // returns: false; myDate.TryGetDayOfMonth(DayOfWeek.Wednesday, 1, out dateOfMonth) // returns: true; dateOfMonth = Wednesday, 1/2/2013 myDate.TryGetDayOfMonth(DayOfWeek.Wednesday, 4, out dateOfMonth) // returns: true; dateOfMonth = Wednesday, 1/23/2013 myDate.TryGetDayOfMonth(DayOfWeek.Wednesday, 5, out dateOfMonth) // returns: true; dateOfMonth = Wednesday, 1/30/2013 // etc

**Find the Monday preceding the third Friday of the month,** get third friday of month java How can I get the 3rd Friday of a month in Python ?, You can use standard python functions to find the third friday of this month: from 2606 | How To select Specific Column From DataTable in C#?; 4382� If the answer is negative, add 7. Finally add that to the first of the month; for my example, the first Friday would be the 3rd. To get the last Friday of the month, find the first Friday of the next month and subtract 7 days. To get the 3rd Friday of the month, add 14 days to the first Friday.

Old post, but I found remarkably few decent answers online for this surely quite common problem! Mark Ransom's answer should be the last word on this algorithm-wise, but here is a C# helper class (in this case I think clearer than extensions) for anyone who wants a quick answer to the common problems of "first day of week in month", "xth day of week in month" and "last day of week in month".

I modified it to return `DateTime.MinValue`

if the Xth day of the week falls outside the provided month rather than wrapping to the next month, because that to me seems more useful.

I've thrown in a LINQPad-runnable example program too.

void Main() { DayOfWeek dow = DayOfWeek.Friday; int y = 2014; int m = 2; String.Format("First {0}: {1}", new object[] { dow, DateHelper.FirstDayOfWeekInMonth(y, m, dow) }).Dump(); "".Dump(); String.Format("Last {0}: {1}", new object[] { dow, DateHelper.LastDayOfWeekInMonth(y, m, dow) }).Dump(); "".Dump(); for(int i = 1; i <= 6; i++) String.Format("{0} #{1}: {2}", new object[] { dow, i, DateHelper.XthDayOfWeekInMonth(y, m, dow, i) }).Dump(); } public class DateHelper { public static DateTime FirstDayOfWeekInMonth(int year, int month, DayOfWeek day) { DateTime res = new DateTime(year, month, 1); int offset = -(res.DayOfWeek - day); if (offset < 0) offset += 7; res = res.AddDays(offset); return res; } public static DateTime LastDayOfWeekInMonth(int year, int month, DayOfWeek day) { DateTime dt = new DateTime(year, month, 1).AddMonths(1); DateTime res = FirstDayOfWeekInMonth(dt.Year, dt.Month, day); res = res.AddDays(-7); return res; } public static DateTime XthDayOfWeekInMonth(int year, int month, DayOfWeek day, int x) { DateTime res = DateTime.MinValue; if (x > 0) { res = FirstDayOfWeekInMonth(year, month, day); if (x > 1) res = res.AddDays((x - 1) * 7); res = res.Year == year && res.Month == month ? res : DateTime.MinValue; } return res; } }

Prints:

First Friday: 07/02/2014 00:00:00 Last Friday: 28/02/2014 00:00:00 Friday #1: 07/02/2014 00:00:00 Friday #2: 14/02/2014 00:00:00 Friday #3: 21/02/2014 00:00:00 Friday #4: 28/02/2014 00:00:00 Friday #5: 01/01/0001 00:00:00 Friday #6: 01/01/0001 00:00:00

**Python: third Friday of a month,** Hi Polly,. I think you were working on the right lines, but starting on the wrong Friday! If you create an event on the 3rd Friday of a month (I chose� Subtract that number from the day you are looking for; for example, if the first day of the month is Wednesday (2) and you're looking for Friday (4), subtract 2 from 4, leaving 2. If the answer is negative, add 7. Finally add that to the first of the month; for my example, the first Friday would be the 3rd.

**create a recurring event on 3rd Friday of the month,** I generated a set of future dates and calculate the day of week and then determine which occurrence of that day it is for any given month. My� How to calculate the third friday of a month? =DATE (YEAR (A1),MONTH (A1),1+7*3)-WEEKDAY (DATE (YEAR (A1),MONTH (A1),8-6)) with any date in A1 will return the third Friday of the month. --.

**Solved: How to retrieve third Friday of a month?,** Given: Friday is called "Friday". The 3rd Friday of the month will always fall from 15th-21st of the month select thedate from yourtable where� The last-friday-of-month word in the calendar vocabulary does most of the work. This program expects the year as a command line argument. USING: calendar calendar.format command-line io kernel math.parser sequences ; IN: rosetta-code.last-fridays (command-line) second string>number <year> 12 <iota> [ months time+ last-friday-of-month ] with map

**Determine 3rd Friday of each month,** Ans: (a) –1, 0, ?, 8, 15, 24 (a) 4 (b) 3 (c) 2 (d) 1 Hint: Check Successive Differences on 2 levels Ans: (b) If the 5th date of a month is Tuesday, what date will be 3� The use of such an approach may be of some value if, for example, one were writing an application that queries a database and the queries were to be bound to return result sets limited to a single month starting always with the first day of the month and ending with the last day of the month.

##### Comments

- Thankss... This is really helpful. :)
- +1 for explaining and
**not giving code**, because that's an algorithm problem here, not a technical one. - This should be the answer, because it's clear, generally applicable, and actually works. I've created a C# class based on it and added it as an answer for anyone who just wants a quickie.
- Thanks for the function! Works nicely and complements Mark's answer.
- You've got a bug. It should be as below. Otherwise I think it's the cleanest solution. // Change to first day of the month DateTime dayOfMonth = instance.AddDays(1 - instance.Day);
- This test: if (occurance < 0 || occurance > 5) will allow a value of #0 for occurrence, which I believe is an error.
- This works but should a TryGet method throw exceptions?I am thinking you modeled this after methods like Int32.TryParse
- Most if not all Microsoft .Net framework methods that start with Try do not throw an exception on normal errors. Instead they return if the attempt is successful as a boolean and return the valid result in an out parameter (like int.tryparse() which throws no exceptions). Now considering I've added additional paramters, it's still best practice to validate parameters and throw exceptions when those parameters are invalid (I probably shouldn't be checking instance, since DateTime cannot be null).
- For example, DateTime.TryParse can throw both an
`ArgumentException`

and a`NotSupportedException`

. - On reflection, it'd be quite nice to change XthDayOfWeekInMonth to accept negative "x"s to indicate "last", "second to last" etc. but I have no personal use case for this, so I haven't done it :o)
- LastDayOfWeekInMonth fails for December because DateTime cannot take Month 13 and figure out that you need January of the next year.
- Well damn, how have I never noticed that? I will edit with a fix