Solved

Datetime picker in VB.net

Posted on 2010-08-23
9
1,010 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 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 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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