Windows Develop Bookmark and Share   
 index > Windows Forms General > calculating the date on friday of every third week of a month
 

calculating the date on friday of every third week of a month

hi,

Is there any easy way of calculating the date on friday of every third week of a month, by using Datetime class or any other class.function??

Thanks,

bilalso  Tuesday, March 28, 2006 2:49 PM

The following test code will fill an array with the dates of the 12 third Frdays of the year:

Private Sub TestTheDates()

Dim TheDates As Date() = CalculateThirdFridays()

For x As Integer = 0 To 11

Console.WriteLine(TheDates(x).ToShortDateString)

Next

End Sub

Private Function CalculateThirdFridays() As Date()

Dim TheDates(11) As Date

Dim TheThirdFriday As Date

For M As Integer = 1 To 12

For d As Integer = 1 To 7

Dim s As String = M & "/" & d & "/06"

Dim TheDate As Date

If Date.TryParse(s, TheDate) Then

If TheDate.DayOfWeek = DayOfWeek.Friday Then

TheThirdFriday = TheDate.AddDays(14)

TheDates(M - 1) = TheThirdFriday

Exit For

End If

End If

Next

Next

Return TheDates

End Function

DMan1  Tuesday, March 28, 2006 3:17 PM

you need a ">="  instead of ">"

Change it to this and it would work

(bias + (bias >= 0 ? 2 : 3) * 7)

 

DateTime first = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);

int bias = DayOfWeek.Friday - first.DayOfWeek;

DateTime result = first.AddDays(bias + (bias >= 0 ? 2 : 3) * 7);

 

but thanks for the code.

bilalso  Tuesday, March 28, 2006 4:21 PM

The following test code will fill an array with the dates of the 12 third Frdays of the year:

Private Sub TestTheDates()

Dim TheDates As Date() = CalculateThirdFridays()

For x As Integer = 0 To 11

Console.WriteLine(TheDates(x).ToShortDateString)

Next

End Sub

Private Function CalculateThirdFridays() As Date()

Dim TheDates(11) As Date

Dim TheThirdFriday As Date

For M As Integer = 1 To 12

For d As Integer = 1 To 7

Dim s As String = M & "/" & d & "/06"

Dim TheDate As Date

If Date.TryParse(s, TheDate) Then

If TheDate.DayOfWeek = DayOfWeek.Friday Then

TheThirdFriday = TheDate.AddDays(14)

TheDates(M - 1) = TheThirdFriday

Exit For

End If

End If

Next

Next

Return TheDates

End Function

DMan1  Tuesday, March 28, 2006 3:17 PM

DateTime first = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);

int bias = DayOfWeek.Friday - first.DayOfWeek;

DateTime result = first.AddDays(bias + (bias > 0 ? 2 : 3) * 7);

Sergey Galich  Tuesday, March 28, 2006 3:22 PM
this code does'nt return correct date for September 2006
bilalso  Tuesday, March 28, 2006 4:12 PM

you need a ">="  instead of ">"

Change it to this and it would work

(bias + (bias >= 0 ? 2 : 3) * 7)

 

DateTime first = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);

int bias = DayOfWeek.Friday - first.DayOfWeek;

DateTime result = first.AddDays(bias + (bias >= 0 ? 2 : 3) * 7);

 

but thanks for the code.

bilalso  Tuesday, March 28, 2006 4:21 PM
ups, sorry, I didn't check it completely, but glad you find use of it!
Sergey Galich  Tuesday, March 28, 2006 4:54 PM

You can use google to search for other answers

Custom Search

More Threads

• I have a beginner problem - textCahnged event
• Killing a thread initiated by BeginInvoke()
• passing a parameter name to a "WITH /end WIth"
• Calling form from another project
• Form Serialization
• Want to Enable/Disable other forms
• What is a Canvas
• What event fires just before Paint?
• ComboBox empty row
• create Custom multiple column comobox in C#