tags:

views:

434

answers:

3

Hi

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?

A: 

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

M.A. Hanin
+1  A: 

just MessageBox.Show(dateTimePicker1.Value.ToString("dd/MM/yyyy"));

PierrOz
I want to do this:datetimepicker.value = 26/03/2010Is there any way I can do that?
salhzmzm
datetimepicker.Value = new DateTime(2010, 3, 26); or datetimepicker.Value = Convert.ToDateTime("26/03/2010");
PierrOz
Use ParseExact like below. Substitute the date string in the first parameter with whatever string variable you have in your program. DateTime dtResult = DateTime.ParseExact("26/03/2010", "d/M/yyyy", CultureInfo.InvariantCulture);See here for more info http://msdn.microsoft.com/en-us/library/system.datetime.parseexact.aspx
Fadrian Sudaman
A: 

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;
Joe