Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to select only month and year from datetimepicker in vb.net
Posted
Updated 26-Nov-20 23:36pm

Date pickers return a DateTime object which has Month and Year properties.
 
Share this answer
 
Use Format property.
C#
dateTimePicker1.Format = DateTimePickerFormat.Custom
dateTimePicker1.CustomFormat = "MMMM yyyy" 'or whatever you want

Ref: http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.format.aspx[^]
 
Share this answer
 
Solution 2 and 3 will crash with an OutOfBounds Exception if the Value has a date which is greater than the number of days in the Next/Previous month and the user changes month using the arrow buttons. For Example when the Value is 10/31/2014 and the control displays October, 2014. When you change the Month to September the Value is set to 9/31/2014 which is an invalid date and causes an exception.

A workaround is
VB
Private Sub SalesDatePicker_ValueChanged(sender As Object, e As EventArgs) Handles SalesDatePicker.ValueChanged
    If SalesDatePicker.Value.Day <> 1 Then
        SalesDatePicker.Value = DateSerial(SalesDatePicker.Value.Year, SalesDatePicker.Value.Month, 1)
    End If
End Sub
 
Share this answer
 
Comments
CHill60 2-Nov-14 16:55pm    
Interesting! Have you considered fleshing this out and posting it as a Tip?
Paul M Cohen 2-Nov-14 18:02pm    
I have never done a tip what does it involve. I have a working solution it would be easy to extract.
CHill60 4-Nov-14 12:45pm    
Go to the Articles section...you'll find inductions there... Sorry I'm using my phone for this and it's not easy to post links... Or type :-)
Paul M Cohen 4-Nov-14 14:16pm    
Thanks I submitted a tip that should be posted shortly.
qulaitks 8-Sep-19 23:40pm    
Good morning
I am having this problem when trying to assign last day of the month.
SalesDatePicker.Value = DateSerial(SalesDatePicker.Value.Year, SalesDatePicker.Value.Month, 1).Addmonths(1).AddDays(-1).
select datetimepicker ---> goto property

format=custom and
CustomFormat=MMM/yyyy
 
Share this answer
 
Comments
fjdiewornncalwe 2-Nov-11 9:54am    
Please don't post duplicate answers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900