SteveJonesMO
10-07-2011, 10:13 PM
Hello:
Many apologies if this is not an appropriate question here... but I have googled my fingers raw and haven't been able to find a script to do what I am looking for. I have little experience with JavaScript.
I have a VBA script (code at the end of this post) that returns the date corresponding to a given occurrence of a given weekday, e.g. Ask it for the 3rd Tuesday in October 2012 and it gives you 16-OCT-2012. It defaults to current year if you don't pass the Year argument.
It's a pretty simple script, but I need to either finds something in JavaScript that does the same thing, or convert the VBA code to JavaScript. I can't help but think that many people have already written scripts that do this. Any chance someone can lead me in a productive direction here?
Regards,
Steve Jones
(update - I noticed the code didn't retain its formatting, so I attached a text file)
-begin VBA code-
Public Function xdate(m As Integer, n As Integer, wd As Integer, Optional y As Integer = 0) As Date
'// m = 1-12 (Jan-Dec), n = 1-5 (1st-4th, last), wd = 1-7 (Sun-Sat), y = 1900-9999 or left blank for current year
Select Case y
Case 0 ' // Default. Use the current year.
y = year(Date)
Case 1900 To 9999 ' // treat the value as an explicit year
Case -100 To -1, 1 To 100 ' // treat the value as an offset to be applied to the current year
y = year(Date) + y
Case Else
' // invalid value. Insert whatever error processing you like.
End Select
Select Case n
Case 1 To 4 '// first through fourth
Case 5 '// last. We need to check to see it fell within the month specified, if not, use 4.
xdate = DateSerial(y, m, (8 - weekday(DateSerial(y, m, 1), (wd + 1) Mod 8)) + ((n - 1) * 7))
If month(xdate) <> m Then
n = 4
End If
Case Else
' // invalid value - insert error processing code here if you like.
' // Probably should check the other arguments as well.
End Select
xdate = DateSerial(y, m, (8 - weekday(DateSerial(y, m, 1), (wd + 1) Mod 8)) + ((n - 1) * 7))
End Function
-end vba code-
Many apologies if this is not an appropriate question here... but I have googled my fingers raw and haven't been able to find a script to do what I am looking for. I have little experience with JavaScript.
I have a VBA script (code at the end of this post) that returns the date corresponding to a given occurrence of a given weekday, e.g. Ask it for the 3rd Tuesday in October 2012 and it gives you 16-OCT-2012. It defaults to current year if you don't pass the Year argument.
It's a pretty simple script, but I need to either finds something in JavaScript that does the same thing, or convert the VBA code to JavaScript. I can't help but think that many people have already written scripts that do this. Any chance someone can lead me in a productive direction here?
Regards,
Steve Jones
(update - I noticed the code didn't retain its formatting, so I attached a text file)
-begin VBA code-
Public Function xdate(m As Integer, n As Integer, wd As Integer, Optional y As Integer = 0) As Date
'// m = 1-12 (Jan-Dec), n = 1-5 (1st-4th, last), wd = 1-7 (Sun-Sat), y = 1900-9999 or left blank for current year
Select Case y
Case 0 ' // Default. Use the current year.
y = year(Date)
Case 1900 To 9999 ' // treat the value as an explicit year
Case -100 To -1, 1 To 100 ' // treat the value as an offset to be applied to the current year
y = year(Date) + y
Case Else
' // invalid value. Insert whatever error processing you like.
End Select
Select Case n
Case 1 To 4 '// first through fourth
Case 5 '// last. We need to check to see it fell within the month specified, if not, use 4.
xdate = DateSerial(y, m, (8 - weekday(DateSerial(y, m, 1), (wd + 1) Mod 8)) + ((n - 1) * 7))
If month(xdate) <> m Then
n = 4
End If
Case Else
' // invalid value - insert error processing code here if you like.
' // Probably should check the other arguments as well.
End Select
xdate = DateSerial(y, m, (8 - weekday(DateSerial(y, m, 1), (wd + 1) Mod 8)) + ((n - 1) * 7))
End Function
-end vba code-