You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Components/Utilities/BindingConverters/DateConverter.cs

29 lines
1.1 KiB

namespace Connected;
2 years ago
/// <summary>
/// A ready made DateTime? to string binding converter with configurable date format and culture
/// </summary>
public class NullableDateConverter : ToStringConverter<DateTime?>
2 years ago
{
public string DateFormat { get; set; }
2 years ago
public NullableDateConverter(string format = "yyyy-MM-dd") => DateFormat = format;
2 years ago
protected override string? ConvertValue(DateTime? value) => value?.ToString(DateFormat, Culture);
2 years ago
protected override DateTime? ConvertValueBack(string? value) => DateTime.ParseExact(value, DateFormat, Culture);
}
/// <summary>
/// A ready made DateTime to string binding converter with configurable date format and culture
/// </summary>
public class DateConverter : ToStringConverter<DateTime>
{
public string DateFormat { get; set; }
public DateConverter(string format = "yyyy-MM-dd") => DateFormat = format;
2 years ago
protected override string? ConvertValue(DateTime value) => value.ToString(DateFormat, Culture);
2 years ago
protected override DateTime ConvertValueBack(string? value) => DateTime.ParseExact(value, DateFormat, Culture);
}