2using System.Globalization;
3using Microsoft.Maui.Controls;
14 public object Convert(
object? value, Type targetType,
object? parameter, CultureInfo culture)
16 if (value is not DateTime DateTime)
19 bool ForceUtc = IsForceUtc(parameter);
21 DateTime Formatted = ForceUtc ? DateTime.ToUniversalTime() : DateTime;
22 bool DateOnly = IsDateOnly(Formatted);
24 string Result = DateOnly
25 ? Formatted.ToString(
"d", culture)
26 : Formatted.ToString(
"G", culture);
28 if (Formatted.Kind == DateTimeKind.Utc || ForceUtc)
29 Result = $
"{Result} (UTC)";
35 public object ConvertBack(
object? value, Type targetType,
object? parameter, CultureInfo culture)
37 if (value is
string s && DateTime.TryParse(s, culture, DateTimeStyles.None, out DateTime Parsed))
40 return DateTime.MinValue;
43 private static bool IsDateOnly(DateTime dateTime)
45 return dateTime.TimeOfDay == TimeSpan.Zero;
48 private static bool IsForceUtc(
object? parameter)
50 if (parameter is
null)
53 if (parameter is
bool BoolParam)
56 if (parameter is
string StrParam)
57 return string.Equals(StrParam,
"utc", StringComparison.OrdinalIgnoreCase);
Converts DateTime values into localized strings, optionally forcing UTC output. Date-only formatting ...
object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)