WPF ISO Date Format Converter

This is a little piece of code I put together for an application. The code is no longer being used but I didn’t want to lose it.
 
 

public sealed class IsoDateValueConverter : IValueConverter {

     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {

          if (value != null && value is DateTime) {

               var date = (DateTime)value;

               return date.ToString(SystemInformation.DateTime.IsoShortDateFormat);

          }

          return string.Empty;

     }

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {

          if (culture == null) {

               throw new ArgumentNullException("culture");

          }

          if (value != null && value is string) {

               return DateTime.ParseExact((string)value, SystemInformation.DateTime.IsoShortDateFormat, CultureInfo.InvariantCulture);

          }

          return null;

     }
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s