Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > DateTimePicker Question
 

DateTimePicker Question

Quick question, I have a datetimepicker control in my application. I only need the date in mm/dd/yyyy format. I set a variable to pull the DateTimePicker1.value and it returns mm/dd/yyyy hh:mmTongue Tieds and AM or PM. I want to truncate the time portion. How do I do that? Thanks.

jimbo101974  Monday, December 31, 2007 5:27 PM

The value property of a DateTimePicker control is a DateTime datatype, which contains both date and time information.

If you want to use this value for display purposes, then you are going to use it in a string format anyway. You can format a DateTime object however you like when calling its .ToString() method.

For example, this would give you the desired value:

Code Block

Dim myFormattedDate As String = DateTimePicker1.Value.ToString("MM/dd/yyyy")

MessageBox.Show(myFormattedDate)

You can also set the datetimepicker controls formatting properties so the appearance of the date in the actual DTP control also matches this format. You can do this at design time by setting the DTPs Format property to Custom and the CustomFormat property to MM/dd/yyyy

Note the capital MM is because lowercase mm is for minutes in date/time formatting literals.

Also note that this DOES NOT change the fact that the value property is still going to have both a date part and time part to it.


This will make the DTP display the date in the format you mentioned. If you do this, you can then also reuse your formatting property from the DTP when you format your output. Like so:

Code Block

Dim myFormattedDate As String = DateTimePicker1.Value.ToString(DateTimePicker1.CustomFormat)

MessageBox.Show(myFormattedDate)

kleinma  Monday, December 31, 2007 7:03 PM

The value property of a DateTimePicker control is a DateTime datatype, which contains both date and time information.

If you want to use this value for display purposes, then you are going to use it in a string format anyway. You can format a DateTime object however you like when calling its .ToString() method.

For example, this would give you the desired value:

Code Block

Dim myFormattedDate As String = DateTimePicker1.Value.ToString("MM/dd/yyyy")

MessageBox.Show(myFormattedDate)

You can also set the datetimepicker controls formatting properties so the appearance of the date in the actual DTP control also matches this format. You can do this at design time by setting the DTPs Format property to Custom and the CustomFormat property to MM/dd/yyyy

Note the capital MM is because lowercase mm is for minutes in date/time formatting literals.

Also note that this DOES NOT change the fact that the value property is still going to have both a date part and time part to it.


This will make the DTP display the date in the format you mentioned. If you do this, you can then also reuse your formatting property from the DTP when you format your output. Like so:

Code Block

Dim myFormattedDate As String = DateTimePicker1.Value.ToString(DateTimePicker1.CustomFormat)

MessageBox.Show(myFormattedDate)

kleinma  Monday, December 31, 2007 7:03 PM

You can use google to search for other answers

Custom Search

More Threads

• Return Type of function is not CLS Compliant .How can Fix this?
• About import texts From out of program
• problem with Win32_ComputerSystem.UserName on W2K hosts
• automating File Server Management
• working with access 2007 Attachment Datatype in vb.net
• Adding a sheet from a template in Excel
• Dochostuiflag_enable_forms_autocomplete
• How to compare a value against multiple values - need help with If statment
• Open A Software Program within VS Created Software
• One Sub to populate different Structures