1

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;
}
Nasenbaer
  • 4,810
  • 11
  • 53
  • 86
Robert
  • 646
  • 4
  • 12
  • 37
  • Are you sure you don't get this error message somewhere else? http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx Or are you using .Net Framework 1.0 ? – Soner Gönül Sep 05 '13 at 14:16

5 Answers5

4

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;
}
marbel82
  • 925
  • 1
  • 18
  • 39
Harsh
  • 3,683
  • 2
  • 25
  • 41
1

You should cast value to DateTime type, because there is not a ToString(String f) method for the type of Object.

if (value is DateTime)
{
   var dateTime = (DateTime)value;
   return dateTime.ToString("dd/MM/yyyy");
}

return string.Empty;
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
0

After check on type of value you need cast it to appropriate type, to be able to perform "ToString" call with format parameter. Try:

if (value is DateTime)
{
    var dateValue = value as DateTime;
    string date = dateValue.ToString("dd/MM/yyyy");
    return date; 
}
ttsushko
  • 21
  • 2
  • you shouldn't use `is` operator. it will be very slowly coz. it equivalent to two-times casting in your case. – Dzmitry Martavoi Sep 05 '13 at 14:19
  • Actually yes, but i wrote answer that fits to author's convert function. In addition it is not good practice to write functions that accept 'object' as parameter and returns also object. – ttsushko Sep 05 '13 at 14:23
  • this is implementation of http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx – Dzmitry Martavoi Sep 05 '13 at 14:25
0

If you are using a Converter on a WPF DatePicker control, you should note that the WPF DatePicker will itself reformat the date despite the converter that you use. You will have to style the Datepicker to include a StringFormat.

There is a related question here: how to show just Month Year string format in a DatePicker which shows an attached property to modify the behaviour of DatePicker to display a custom format. This is needed because of a deficiency in the WPF Datepicker control itself.

Also note there are some caveats, notably the DatePicker will flicker between its default stringformat and the one you apply! I answered in the above question a workaround for to apply a custom Format to WPF Datepicker without the flicker.

Hope you find the solution you are looking for.

Community
  • 1
  • 1
Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
-2
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (value is DateTime)
    {
        string date=value.Date.ToShortDateString();
        return (date);
    }

    return string.Empty;
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Nithin Nayagam
  • 458
  • 2
  • 5
  • 9