I have DateTimePicker on my form and I set a value to the custom format property to "dd/MM/yyyy" ant when I run this code:
MessageBox.Show(dateTimePicker1.Value.ToString());
I get this value : "3/26/2010 1:26 PM".
How I can remove the time part from value.
I know we can use this method
dateTimePicker1.Value.ToShortDateString();
but I want to set the value property to this format "dd/MM/yyyy" so the output will be like this "26/3/2010", because I want to store the value in my DB (SQL)
How I can do that?
The DateTimePicker control is used to allow the user to select a date and time, and to display that date and time in the specified format. The DateTimePicker control makes it easy to work with dates and times because it handles a lot of the data validation automatically.
The DatePicker class has a property, SelectedDate, which enable you to get or set the selected date. If there is none selected, it means its value will be null.
Use dateTimePicker1.Value.Date
to get the Date part of this DateTime value.
Do notice though, if you mean to parse into strings, that using dateTimePicker1.Value.Date.ToString
will result with the "26/03/2010 00:00:00" string, while using something like MyString = CStr(dateTimePicker1.Value.Date)
will result in MyString being "26/03/2010".
If you have string as datatype in database:
dateTimePicker1.Value.Date.ToString("d");
If datatype is DateTime:
dateTimePicker1.Value.Date;
Both will return date in dd/mm/yyyy format without time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With