Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set DateTime format in IValueConverter and return string value?

I want a specific format in IValueConverter and return a string. Like "MMMyyyy" How can I get this?

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    var item = (DateTime)value;
    if (item != null)
    {
        return item.ToString();
    }
    return null;
}
like image 270
Raino Avatar asked Oct 23 '25 19:10

Raino


2 Answers

Please try below

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
     var item = (DateTime)value;
     if (item != null)
     {
         return item.ToString("MMMyyyy");
     }
     return null;
}
like image 57
Sachin Avatar answered Oct 26 '25 11:10

Sachin


Do you really need an IValueConverter here? If you want this for a binding to display the databound DateTime with a specific format in WPF, you can use the StringFormat property of the Binding

Article about StringFormat

like image 39
Stephan Bauer Avatar answered Oct 26 '25 11:10

Stephan Bauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!