Date Projections

VB Snippet

Date Projections

Ever wanted to know the date from given criteria, e.g the 1st Sunday in December 2003.

Try it here, although this sample is written in vbscript, the routine is the same



Click to see versions in    vbscript     java/swing     java/applet

VB Code

Start a new project, and add to Form1

4 x List boxes (set up to look like above)
1 x Command button
1 x Label

Option Explicit
Private Sub Form_Load()
Dim i As Integer
List1.AddItem "1st"
List1.AddItem "2nd"
List1.AddItem "3rd"
List1.AddItem "4th"
List1.AddItem "Last"
For i = 1 To 12
 If i < 8 Then List2.AddItem WeekdayName(i, , vbSunday)
 List3.AddItem MonthName(i)
 List4.AddItem (1999 + i)
End Sub
Private Sub Command1_Click()
Dim yr As Integer: yr = CInt(List4.Text)
Dim mth As Integer: mth = List3.ListIndex + 1
Dim dayOfWeek As Integer: dayOfWeek = List2.ListIndex + 1
Dim weekOfMonth As Integer: weekOfMonth = List1.ListIndex + 1
Dim firstOfMonth As Date
Dim dateRequired As Date
If weekOfMonth = 5 Then
  mth = mth + 1
  weekOfMonth = 0
End If
firstOfMonth = DateSerial(yr, mth, 1)
dateRequired = firstOfMonth - Weekday(firstOfMonth, (dayOfWeek Mod 7) + 1) + (7 * weekOfMonth)
Label1.Caption = Format$(dateRequired, "dd mmm yyyy")
End Sub


back     top   main page    vb snippets page      java snippets page     vbscript snippets page   email    Page last modified