Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Third Friday of a month 1

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Hello

Given a date. How can we get third friday of that month.

Any suggestions..

-DNG
 
Hi Guys,

Ignore this post. I figured how to find the third friday of the month. Let me know if someone needs it.

Thanks for looking...

-DNG
 
Always good to post your solutions, even if you find them yourself. The next person after you may find that useful information...

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
As an example, here is one example. Probably not the most efficient and it does not have error checking to ensure you do not move into a new month (so if you ask for the 7th Friday of a month, you end up with the 7th Friday after the first day of the month you input) but it does the job.

Code:
//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;
        }

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
here is what I did:

Code:
    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;
     }

-DNG
 
Nice....Your solution is much more concise and easier to read.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top