Thursday, October 2, 2008

format date in xaml

xmlns:local="clr-namespace:.Utility">






//DateTime format Binding
namespace .Utility
{
///
/// Utility class to convert the date format
///

public class PositionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
//converts the format during Binding
return String.Format("{0:s}", value).Remove(10);
}
return "";
}

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

throw new Exception("The method or operation is not implemented.");
}

}
}

date format tht will help you out,

DateTime dateTime = DateTime.Now;
C.WriteLine ("d = {0:d}", dateTime ); // mm/dd/yyyy
C.WriteLine ("D = {0:D}", dateTime ); // month dd, yyyy
C.WriteLine ("f = {0:f}", dateTime ); // day, month dd, yyyy hh:mm
C.WriteLine ("F = {0:F}", dateTime ); // day, month dd, yyyy HH:mm:ss AM/PM
C.WriteLine ("g = {0:g}", dateTime ); // mm/dd/yyyy HH:mm
C.WriteLine ("G = {0:G}", dateTime ); // mm/dd/yyyy hh:mm:ss
C.WriteLine ("M = {0:M}", dateTime ); // month dd
C.WriteLine ("R = {0:R}", dateTime ); // ddd Month yyyy hh:mm:ss GMT
C.WriteLine ("s = {0:s}", dateTime ); // yyyy-mm-dd hh:mm:ss (Sortable)
C.WriteLine ("t = {0:t}", dateTime ); // hh:mm AM/PM
C.WriteLine ("T = {0:T}", dateTime ); // hh:mm:ss AM/PM