DotNetGnat
Programmer
Hello
Given a date. How can we get third friday of that month.
Any suggestions..
-DNG
Given a date. How can we get third friday of that month.
Any suggestions..
-DNG
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
//usage: this.textBox2.Text = GetWeekdayOfMonth(Convert.ToDateTime(this.textBox1.Text), DayOfWeek.Friday, 3).ToShortDateString();
public DateTime GetWeekdayOfMonth(DateTime inputDate, DayOfWeek weekday, int weekNumber)
{
DateTime startDate;
DateTime currentDate;
int weekdayCount = 0;
startDate = inputDate;
currentDate = new DateTime(startDate.Year, startDate.Month, 1);
while (weekdayCount < weekNumber)
{
if (currentDate.DayOfWeek == weekday)
{
weekdayCount++;
if (weekdayCount == weekNumber)
{
break;
}
}
currentDate = currentDate.AddDays(1);
}
return currentDate;
}
public static DateTime GetThirdFriday(DateTime sdate)
{
DateTime thirdFriday = new DateTime(sdate.Year, sdate.Month, 1);
while (thirdFriday.DayOfWeek != DayOfWeek.Friday)
{
thirdFriday = thirdFriday.AddDays(1);
}
thirdFriday = thirdFriday.AddDays(14);
return thirdFriday;
}