0

I am attempting to add a Convert funtion to my XAML/C# application. However, the "value" object in the code below is presenting the data back as "2014-12-14", which is generating a runtime error, "Specified cast is not valid". Can anyone help me with the converstion string? I tried ParseExact, but with no success.

      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                DateTime date = (DateTime)value;
                return date.ToShortDateString();

            }
            catch (Exception ex)
            {
                return DateTimeOffset.MinValue;
            }

        }
2
Contributors
6
Replies
40
Views
3 Years
Discussion Span
Last Post by ChrisHunter
0

I had a similar problem a couple of weeks ago and this was my solution

DateTime.ParseExact(value.Substring(0, 10), "dd/mm/yyyy", null).Date.ToShortDateString();

You should just have to change the format string ("dd/mm/yyyy") to "yyyy/mm/dd".

Hope this helps...

Edited by ChrisHunter

0

That's great thank you Chris. Unfortunately, it doesn't like the .Substring command.

1

Try value.ToString().Substring(0, 10). If the object passed in as value param isn't a string it won't work. What will you pass in as value param?

You might also need to change the string format to include - instead of /.

0

Could you mark this thread as solved please? That way people with the same problem will know this thread may contain the solution they're looking for.

Cheers.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.