Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert datetime to string without default time value [duplicate]

When I convert selected date of a DateTimePicker to a string it gives value like "01/03/2013 12:00:00 AM". How I can drop the time value and select only "01/03/2013" part?

like image 846
Madusha Avatar asked Jan 17 '26 21:01

Madusha


1 Answers

try this ToShortDateString:

var datestring = datePicker.Value.ToShortDateString();

It will

Converts the value of the current DateTime object to its equivalent short date string representation.

The string returned by the ToShortDateString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo object. For example, for the en-US culture, the standard short date pattern is "M/d/yyyy"; for the de-DE culture, it is "dd.MM.yyyy"; for the ja-JP culture, it is "yyyy/M/d". The specific format string on a particular computer can also be customized so that it differs from the standard short date format string.

if you want excactly dd/MM/yyyy format use custom ToString() like:

var datestring = datePicker.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
like image 61
Kamil Budziewski Avatar answered Jan 19 '26 14:01

Kamil Budziewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!