Link to home
Start Free TrialLog in
Avatar of alsam
alsam

asked on

Datetime picker in VB.net

Hi,
I have 4 datetime picker objects on my win form.
On form load I would like to set those 4 datetime pickers like:
1. First picker as first date of the current month
2. Second picker as last date of current month
3. third picker as last date of previous month
4. Forth picker as first date of previous month...

Can someone help me to make this working. I would appreciate your time and effort a lot.
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can do:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim CurrentFirstOfMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
        DateTimePicker1.Value = CurrentFirstOfMonth
        DateTimePicker2.Value = CurrentFirstOfMonth.AddMonths(1).AddDays(-1)
        DateTimePicker3.Value = CurrentFirstOfMonth.AddDays(-1)
        DateTimePicker4.Value = CurrentFirstOfMonth.AddMonths(-1)
    End Sub

End Class

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Simply you can use Dateserial like :
        Me.DateTimePicker1.Value = DateSerial(Year(Now), Month(Now), 1)
        Me.DateTimePicker2.Value = DateSerial(Year(Now), Month(Now) + 1, 1 - 1)
        Me.DateTimePicker3.Value = DateSerial(Year(Now), Month(Now), 1 - 1)
        Me.DateTimePicker4.Value = DateSerial(Year(Now), Month(Now) - 1, 1)

Open in new window

Avatar of magdy99
magdy99

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim m, y As Integer

        m = DateTime.Now.Month
        y = DateTime.Now.Year
        Dim s As String = "#" & m & "/" & "1" & "/" & y & "#"
        Dim d As DateTime = CType(s, DateTime)
        '  First picker as first date of the current month

        DateTimePicker1.Value = d
        'Second picker as last date of current month
        If m = 1 Or m = 3 Or m = 5 Or m = 7 Or m = 8 Or m = 10 Or m = 12 Then
            Dim s2 As String = "#" & m & "/" & "31" & "/" & y & "#"
            Dim d2 As DateTime = CType(s2, DateTime)
            DateTimePicker2.Value = d2

        End If
        If m = 4 Or m = 6 Or m = 9 Or m = 11 Then
            Dim s2 As String = "#" & m & "/" & "30" & "/" & y & "#"
            Dim d2 As DateTime = CType(s2, DateTime)
            DateTimePicker2.Value = d2

        End If
        If m = 2 Then
            Dim s2 As String = "#" & m & "/" & "28" & "/" & y & "#"
            Dim d2 As DateTime = CType(s2, DateTime)
            DateTimePicker2.Value = d2

        End If
        'third picker as last date of previous month
        Dim s3 As String = "#" & m - 1 & "/" & "1" & "/" & y & "#"
        Dim d3 As DateTime = CType(s3, DateTime)
        DateTimePicker3.Value = d3
        'Forth picker as first date of previous month
        If m = 1 Or m = 3 Or m = 5 Or m = 7 Or m = 8 Or m = 10 Or m = 12 Then
            Dim s4 As String = "#" & m - 1 & "/" & "31" & "/" & y & "#"
            Dim d4 As DateTime = CType(s4, DateTime)
            DateTimePicker4.Value = d4
        End If
        If m = 4 Or m = 6 Or m = 9 Or m = 11 Then
            Dim s4 As String = "#" & m - 1 & "/" & "31" & "/" & y & "#"
            Dim d4 As DateTime = CType(s4, DateTime)
            DateTimePicker4.Value = d4
        End If
        If m = 2 Then
            Dim s4 As String = "#" & m - 1 & "/" & "31" & "/" & y & "#"
            Dim d4 As DateTime = CType(s4, DateTime)
            DateTimePicker4.Value = d4
        End If
    End Sub
End Class
Good grief magdy99....  =\
if current month is january, you should update the Year part.
...who is that directed at sedgwick?
my bad.