|
-
Apr 6th, 2009, 01:59 AM
#1
Thread Starter
New Member
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.
-
Apr 6th, 2009, 02:24 AM
#2
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
Last edited by danasegarane; Apr 6th, 2009 at 02:43 AM.
Please mark you thread resolved using the Thread Tools as shown
-
Apr 6th, 2009, 07:12 AM
#3
Re: DTPicker setting and changing dates.
rowant: Are you using VB.NET?
-
Apr 6th, 2009, 01:19 PM
#4
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|