Results 1 to 4 of 4

Thread: DTPicker setting and changing dates.

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    15

    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.

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    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

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: DTPicker setting and changing dates.

    rowant: Are you using VB.NET?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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
  •  



Click Here to Expand Forum to Full Width