Solved

Datetime picker in VB.net

Posted on 2010-08-23
9
996 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:alsam
9 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 33508195
1. dateTimePicker1.Value = new DateTime(dateTime.Year, dateTime.Month, 1);2. DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);    dateTimePicker2.Value  = firstDayOfTheMonth.AddMonths(1).AddDays(-1);3. DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);   firstDayOfTheMonth.AddMonths(-1);    dateTimePicker2.Value  = new firstDayOfTheMonth.AddMonths(1).AddDays(-1);4. DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);   firstDayOfTheMonth.AddMonths(-1);    dateTimePicker2.Value  = new DateTime(firstDayOfTheMonth.Year, firstDayOfTheMonth.Month, 1);
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33508200
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

0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 250 total points
ID: 33508232
first day of current month:
DateTime firstDayOfCurrentMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

last day of current month:
DateTime lastDayOfCurrentMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddMonths(1).AddDays(-1);

first day of previous month:
DateTime firstDayOfCurrentMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month-1, 1);

last day of previous month:
DateTime lastDayOfCurrentMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month-1, DateTime.Now.Day).AddMonths(1).AddDays(-1);

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33508237
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

0
 

Expert Comment

by:magdy99
ID: 33508252
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33508266
Good grief magdy99....  =\
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33508267
if current month is january, you should update the Year part.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33508277
...who is that directed at sedgwick?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33508315
my bad.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question