예제 #1
0
 /// <summary>
 /// Gets the internal input date format based on the input mode.
 /// </summary>
 /// <param name="inputMode">Input mode.</param>
 /// <returns>Valid date format.</returns>
 public static string GetInternalDateFormat(DateInputMode inputMode)
 {
     return(inputMode switch
     {
         DateInputMode.DateTime => InternalDateTimeFormat,
         DateInputMode.Month => InternalMonthFormat,
         _ => InternalDateFormat,
     });
예제 #2
0
        public static string ToDateInputMode(this DateInputMode dateInputMode)
        {
            switch (dateInputMode)
            {
            case DateInputMode.DateTime:
                return("datetime-local");

            case DateInputMode.Date:
            default:
                return("date");
            }
        }
예제 #3
0
        public static bool TryParseDate <TValue>(string value, DateInputMode inputMode, out TValue result)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                result = default;
                return(false);
            }

            var supportedFormats = inputMode == DateInputMode.DateTime
                ? SupportedDateTimeFormats
                : SupportedDateFormats;

            var type = Nullable.GetUnderlyingType(typeof(TValue)) ?? typeof(TValue);

            if (type == typeof(DateTime) && DateTime.TryParseExact(value, supportedFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dt))
            {
                result = (TValue)(object)dt;
                return(true);
            }

            if (type == typeof(DateTime) && DateTimeOffset.TryParse(value, out var dto))
            {
                result = (TValue)(object)dto.DateTime;
                return(true);
            }

            if (type == typeof(DateTimeOffset) && DateTimeOffset.TryParse(value, out var dto2))
            {
                result = (TValue)(object)dto2;
                return(true);
            }

            result = default;

            return(false);
        }