1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Data;
- namespace WPfPointInfo.InfoProperty
- {
- public class DateConvert : IValueConverter
- {
- public static readonly DateConvert Date = new DateConvert();
- public static readonly DateConvert DateTime = new DateConvert() { DataTimeFormat = "yyyyMMddHHmmss" };
- public DateConvert()
- {
- DataTimeFormat = "yyyyMMdd";
- }
- public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- DateTime dateTime = System.DateTime.Now;
- if (value == null)
- return dateTime;
- if (System.DateTime.TryParseExact(value.ToString(), DataTimeFormat, CultureInfo.CurrentCulture,
- DateTimeStyles.AssumeLocal, out dateTime))
- {
- return dateTime;
- }
- return System.DateTime.Now;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- try
- {
- DateTime dateTime = (DateTime)value;
- return dateTime.ToString(DataTimeFormat);
- }
- catch (Exception)
- {
- }
- return string.Empty;
- }
- public string DataTimeFormat { get; set; }
- }
- }
|