Convert phrase "third Monday in January" into its actual date

  • Thread starter Brandon J. Acquaviva
  • Start date
B

Brandon J. Acquaviva

I need a way to convert the phrase "third Monday in January" into its actual
date for that given year. Any clue as to how to do this?
 
K

Karl E. Peterson

Brandon J. Acquaviva said:
I need a way to convert the phrase "third Monday in January" into its actual
date for that given year. Any clue as to how to do this?

Heh, lots of lookups. <g> Will it really be textual? If you can reduce it to
numbers, it won't be that difficult. But it will be tedious. A good algorithm can
be taken from those that convert the dates daylight/standard times change over
(typically things like "first Sunday in April" or "last Sunday in October". Here's a
routine from TimeZone.zip on http://www.mvps.org/vb/samples.htm:

Private Function tzDate(st As SYSTEMTIME) As Date
Dim i As Long
Dim n As Long
Dim d1 As Long
Dim d2 As Long

' This function supports two date formats. Absolute format
' specifies an exact date and time when standard time
' begins. In this form, the wYear, wMonth, wDay, wHour,
' wMinute, wSecond, and wMilliseconds members of the
' SYSTEMTIME structure are used to specify an exact date.
If st.wYear Then
tzDate = _
DateSerial(st.wYear, st.wMonth, st.wDay) + _
TimeSerial(st.wHour, st.wMinute, st.wSecond)

' Day-in-month format is specified by setting the wYear
' member to zero, setting the wDayOfWeek member to an
' appropriate weekday, and using a wDay value in the
' range 1 through 5 to select the correct day in the
' month. Using this notation, the first Sunday in April
' can be specified, as can the last Thursday in October
' (5 is equal to "the last").
Else
' Get first day of month
d1 = DateSerial(Year(Now), st.wMonth, 1)
' Get last day of month
d2 = DateSerial(Year(d1), st.wMonth + 1, 0)

' Match weekday with appropriate week...
If st.wDay = 5 Then
' Work backwards
For i = d2 To d1 Step -1
If WeekDay(i) = (st.wDayOfWeek + 1) Then
Exit For
End If
Next i
Else
' Start at 1st and work forward
For i = d1 To d2
If WeekDay(i) = (st.wDayOfWeek + 1) Then
n = n + 1 'incr week value
If n = st.wDay Then
Exit For
End If
End If
Next i
End If

' Got the serial date! Just format it and
' add in the appropriate time.
tzDate = i + _
TimeSerial(st.wHour, st.wMinute, st.wSecond)
End If
End Function

That's the guts of it. (You will need to watch how Year is handled, there,
presumably passing your own in, rather than using the current value.) Will you have
trouble converting the phrase into numbers?

Later... Karl
 

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

Top