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/y开发者_开发知识库yyy" 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?
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.
I assume you initialized the DateTimePicker with the current date and time:
dateTimePicker1.Value = DateTime.Now;
Instead, initialize it with the current date:
dateTimePicker1.Value = DateTime.Today;
What happens is as follows:
If the user selects a date, then the
DateTimePicker.Value
property will always return a date with no time of day component, andDateTime.Kind
set toUnspecified
.If the user doesn't select a date, then the
DateTimePicker.Value
property will return the value used to initialize theDateTimePicker.Value
property. If you initialize it withDateTime.Now
, it will have a time of day component, andDateTime.Kind
will be set toLocal
.
just MessageBox.Show(dateTimePicker1.Value.ToString("dd/MM/yyyy"));
dateTimePicker1.Value.ToString("dd-MM-yyyy")
This should work
You can set DateTimePicker "Format" to "Short" in Properties window (under Appearance)
精彩评论