Date math

A

Andrew

Having trouble with some date computations. I need to get the date of a
specific day within a month - the 1st Thursday or the 2nd Monday, etc. I
found an article ( http://www.dotnetspider.com/kb/Article1252.aspx) that
gives examples using on the DateAdd() and DateDiff() functions, but it seems
that not all of the examples work as described.... and of course, the last
example in the article is very close to what I need yet does not work (I
keep getting 08/11/0287 when I run it).

My goal is to have a way I can tell if it is a specific day of the month so
I can display a message to the user. I am just not able to get my head
around the necessary date math.

Any help is greatly appreciated. Thanks

-- Andrew
 
G

Guest

Hi;

I don't think there is a function to get the 2nd Tuesday of the month. But
it should be simple (this code I just typed so it may be mis-spelled):

DateTime dt = ...get date with month you want.
dt.Day = 8; // very first day that could be the 2nd of any day of the month
while (dt.DayOfWeek != DayOfWeek.Tuesday)
dt = dt.AddDay(1);

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
G

Guest

Howdy,

I wrote a generic function that should do the job:

Public Shared Function FirstDayOfTheMonth( _
ByVal [date] As DateTime, _
ByVal dayOfWeek As DayOfWeek) As DateTime

Dim firstDay As New DateTime([date].Year, [date].Month, 1)

Return firstDay.AddDays(( _
CType(dayOfWeek, Integer) - _
CType(firstDay.DayOfWeek, Integer) + 7) Mod 7)

End Function

hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,808
Messages
2,569,685
Members
45,450
Latest member
EileenShol

Latest Threads

Top