?
Solved

Datetime picker in VB.net

Posted on 2010-08-23
9
Medium Priority
?
1,031 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 1000 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 86

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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

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 86

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

741 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