Search code examples
c#datetimevisual-studio-2012datetimepicker

Convert datetime to string without default time value


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?


Solution

  • 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);