469,275 Members | 1,801 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Last Friday of each month

ats
Does anybody have any sample code for calculating the date for teh last
Friday in each month.

TIA
--
ats@jbex

When an old lady got hit by a truck
I saw the wicked gleam in your eyes

Adam and The Ants - Whip In My Valise
May 23 '07 #1
3 5562
This function returns the date of the last friday of the specified
month/year combination.

In countries that were under the influence of Great Britain in 1752 or were
established after 1752 by the British then the function holds good for any
month/year combination from January 1753 onwards.

For other countries one needs to determine when the calendar was last
adjusted for the lack of leap-years and NOT use the function for month/year
combinations before that point in time.

Public Shared Function LastFridayOfMonth(ByVal month As Integer, ByVal
year As Integer) As DateTime

' Calculate the date for the last day of the specified month/year
combination.
Dim _date As DateTime = (New DateTime(year, month,
1)).AddMonths(1).AddDays(-1)

' Grab the DayOfWeek for that date (0 = Sunday ... 6 = Saturday)
Dim _dayofweek As Integer = CType(_date.DayOfWeek, Integer)

' Calculate the offset to the preceeding Friday
Dim _offset As Integer = _dayofweek - (_dayofweek + 1) * 2
If _offset < -6 Then _offset += 7

' Return the adjusted date
Return _date.AddDays(_offset)

End Function
"ats@jbex" <al***@allenjones.NOSPAM.co.PLEASE.ukwrote in message
news:3l****************************@40tude.net...
Does anybody have any sample code for calculating the date for teh last
Friday in each month.

TIA
--
ats@jbex

When an old lady got hit by a truck
I saw the wicked gleam in your eyes

Adam and The Ants - Whip In My Valise
May 23 '07 #2
ats
On Wed, 23 May 2007 23:47:29 +1200, Stephany Young wrote:
Public Shared Function LastFridayOfMonth(ByVal month As Integer, ByVal
year As Integer) As DateTime

' Calculate the date for the last day of the specified month/year
combination.
Dim _date As DateTime = (New DateTime(year, month,
1)).AddMonths(1).AddDays(-1)

' Grab the DayOfWeek for that date (0 = Sunday ... 6 = Saturday)
Dim _dayofweek As Integer = CType(_date.DayOfWeek, Integer)

' Calculate the offset to the preceeding Friday
Dim _offset As Integer = _dayofweek - (_dayofweek + 1) * 2
If _offset < -6 Then _offset += 7

' Return the adjusted date
Return _date.AddDays(_offset)

End Function
Thanks for the code. I will try that also. No worries re the earlier dates.
It's for a web page so our employees will know what date is payday for any
given month.

Thanks
--
ats@jbex

The cracks between the paving stones
Look like rivers of flowing veins.
Strange people who know me
Peeping from behind every window pane

The Who - The Real Me
May 23 '07 #3
ats@jbex wrote:
Thanks for the code. I will try that also. No worries re the earlier
dates. It's for a web page so our employees will know what date is
payday for any given month.
If the last Friday in a month falls on a [national | bank | whatever]
holiday, is that still payday, or is the payday moved forward?

Andrew
May 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sasidhar Parvatham | last post: by
10 posts views Thread by sherifffruitfly | last post: by
1 post views Thread by Svetac | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.