comparison MetroWpf/MetroWpf.Xaml/Converters/DateTimeToStringConverter.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
comparison
equal deleted inserted replaced
14:741981715d94 15:060f02cd4591
1 namespace MetroWpf.Xaml.Converters
2 {
3 using System;
4 using System.Globalization;
5 using System.Text;
6 using System.Windows.Data;
7
8 /// <summary>
9 /// Converts a Date and Time into a string for display.
10 /// </summary>
11 public class DateTimeToStringConverter : IValueConverter
12 {
13 #region IValueConverter Members
14
15 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
16 {
17 var dateBuilder = new StringBuilder();
18 var dtValue = (DateTime)value;
19
20 DateTime now = DateTime.Now;
21 bool isToday = now.Date == dtValue.Date;
22 bool wasYesterday = now.Subtract(TimeSpan.FromDays(1)).Date == dtValue.Date;
23
24 if (isToday || wasYesterday)
25 {
26 dateBuilder.Append(dtValue.ToString("t", culture));
27 if (wasYesterday)
28 {
29 dateBuilder.Append(" yesterday");
30 }
31 }
32 else
33 {
34 dateBuilder.Append(dtValue.ToString("g"));
35 }
36
37 return dateBuilder.ToString();
38 }
39
40 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
41 {
42 throw new NotImplementedException();
43 }
44
45 #endregion
46 }
47 }