Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set datetimepicker value to only date in (.NET)

Tags:

c#

.net

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?

like image 588
salhzmzm Avatar asked Mar 26 '10 11:03

salhzmzm


People also ask

What is a date time picker?

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.

Can DatePicker be null?

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.


2 Answers

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".

like image 178
M.A. Hanin Avatar answered Oct 13 '22 22:10

M.A. Hanin


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.

like image 24
Trushant04 Avatar answered Oct 13 '22 22:10

Trushant04