Robert Robert - 7 days ago 6
C# Question

datetime converter WPF

I have this converter that i made to get the current time once the date is selected from the DataPicker. In string Date i am getting the value that was selected from the DatePicker, but i cant seem to only get the date. The format that is coming into the Value property is 9/24/2013 12:00:00 I would like it to be 9/24/2013

the error i am getting is "Error 122 No overload for method 'ToString' takes 1 argument"

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is DateTime)
{
string date = value.ToString("d/M/yyyy");
return (date);
}

return string.Empty;
}

Answer

You need to cast it to DateTime first:

public object Convert(object value, Type targetType, object parameter,
                      System.Globalization.CultureInfo culture)
{
    if (value is DateTime)
    {
        DateTime test = (DateTime) value;
        string date = test.ToString("d/M/yyyy");
        return date;
    }

    return string.Empty;
}
Comments