Solved

Datetime picker in VB.net

Posted on 2010-08-23
9
981 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now