DTPicker setting and changing dates.
Hi Everyone,
I some code that changes a DTPicker to monday in the current week below.
Code:
DTPicker1.Value = DateAdd("d", -(Weekday(CDate(Date), vbMonday) Mod 7) + 1, CDate(Date))
What do I change to make it to last weeks monday and next mondays date? I have two buttons that will say "next monday" "previous monday". Any help would be appreciated. Thanks.
Re: DTPicker setting and changing dates.
Next Monday
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'Next Monday
Dim dayofweek As Integer = DateTimePicker1.Value.DayOfWeek
'If sunday move by one
If dayofweek = 0 Then
DateTimePicker1.Value = DateTimePicker1.Value.AddDays(+1)
Else
DateTimePicker1.Value = DateTimePicker1.Value.AddDays((7 - dayofweek) + 1)
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'Previous Monday
Dim dayofweek As Integer = DateTimePicker1.Value.DayOfWeek
If dayofweek = 0 Then
DateTimePicker1.Value = DateTimePicker1.Value.AddDays(-6)
Else
DateTimePicker1.Value = DateTimePicker1.Value.AddDays(-(dayofweek - 1))
End If
End Sub
Re: DTPicker setting and changing dates.
rowant: Are you using VB.NET?
Re: DTPicker setting and changing dates.
If you are using VB6, the second parameter to DateAdd is the amount of units (with "d" that is days) to add, so just change (or add to) the +1, eg:
Next Monday:
Code:
DTPicker1.Value = DateAdd("d", -(Weekday(Date, vbMonday) Mod 7) + 1 + 7, Date)
Last Monday:
Code:
DTPicker1.Value = DateAdd("d", -(Weekday(Date, vbMonday) Mod 7) + 1 - 7, Date)
Note that there is no need to use CDate(Date) , as Date is already that data type - just use Date by itself