Facebook Twitter Gplus LinkedIn YouTube Google Maps RSS
Home Technical IValueConverter: DateTime to Date Converter
formats

IValueConverter: DateTime to Date Converter

Published on June 30, 2010 by in Technical

using System;
using System.Windows;
using System.Windows.Data;
using System.Globalization;

namespace Ravi.Infrastructure.Converters
{
///

/// This class converts DateTime to Date and viceversa
/// Convert Ex: 09/09/2009 10:30:00 AM to 09/09/2009
/// ConvertBack Ex: 09/09/2009 to 09/09/2009 12:00:00 AM
///

public class DateConverter : IValueConverter
{
#region IValueConverter Members

///

/// Convert from a DateTime to a Date(String)
/// /// DateTime /// String Type /// Formatting Parameter /// Culture to use /// String

public object Convert(object value,Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value==null || (DateTime)value==DateTime.MinValue) return null;

DateTime dt = (DateTime) value;

return dt.ToString((string) parameter, culture);
}

///

/// Convert back to DateTime
/// /// String /// String Type /// Formatting Parameter /// Culture to use /// DateTime

public object ConvertBack(object value,Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string val = (string) value;

DateTime outDate;

if (DateTime.TryParse(val,culture,DateTimeStyles.None,out outDate))
{
return outDate;
}

return null;
// return DependencyProperty.UnsetValue;
}
#endregion
}
}

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

12,871 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© All rights reserved to Cyberbrutus. 2012
credit

Switch to our mobile site