Convert datetime to string without default time value

20,113

Solution 1

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

Solution 2

use it

DateTime.Now.ToString("dd/MM/yyyy");
Share:
20,113
Madusha
Author by

Madusha

I'm working as a Trainee Software Developer. I have worked on C#, Java, WPF.

Updated on October 31, 2020

Comments

  • Madusha
    Madusha over 3 years

    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?

  • Tim Schmelter
    Tim Schmelter over 10 years
    Note that your last approach could fail if the current culture doesn't use / as date separator but he wants to enforce it. Use CultureInfo.InvariantCulture as second parameter in ToString then. Btw, ToShortdateString is the same as ToString("d"). See: msdn.microsoft.com/en-us/library/az4se3k1.aspx#ShortDate
  • Kamil Budziewski
    Kamil Budziewski over 10 years
    @TimSchmelter of course, thanks I'll edit it