public class DateTimeConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values != null)
{
DateTime test = (DateTime) value ;
string date = test.ToString("d/M/yyyy");
return (date);
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
I made this converter to get the current time once the date is selected from the DatePicker. In string Date I get the value that was selected from the DatePicker, but I can't seem to only get the date. The format that is coming into the Value property is 9/24/2013 12:00:00, but I would like it to be 9/24/2013. I have already asked a similar question at datetime converter WPF, but none of the provided answers worked. I get the same error: Specified cast is not valid.
You dont need a converter for doing this. You can use the
StringFormat
in binding itself to format your selected datetime to show just date in mm/dd/yyyy format.I tested with this code and it is working fine. XAML:
Model:
ViewModel has list of TestData to be bound to DataGrid:
Try this.