Solved

Datetime picker in VB.net

Posted on 2010-08-23
9
1,018 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 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