/// <summary> /// Returns a string expression representing a standardized date- and time value. /// </summary> /// <param name="value">The <see cref="DateTime"/> value to be formatted.</param> /// <param name="pattern">The standardized patterns to apply on <paramref name="value"/>.</param> /// <param name="provider">An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.</param> /// <returns>Returns a string expression representing a date- and time value.</returns> public static string FromDateTime(DateTime value, DateTimeFormatPattern pattern, IFormatProvider provider) { DateTimeFormatInfo formatInfo = DateTimeFormatInfo.GetInstance(provider); switch (pattern) { case DateTimeFormatPattern.LongDate: return(value.ToString(formatInfo.LongDatePattern, formatInfo)); case DateTimeFormatPattern.LongDateTime: return(value.ToString(string.Format(formatInfo, "{0} {1}", formatInfo.LongDatePattern, formatInfo.LongTimePattern), formatInfo)); case DateTimeFormatPattern.LongTime: return(value.ToString(formatInfo.LongTimePattern, formatInfo)); case DateTimeFormatPattern.ShortDate: return(value.ToString(formatInfo.ShortDatePattern, formatInfo)); case DateTimeFormatPattern.ShortDateTime: return(value.ToString(string.Format(formatInfo, "{0} {1}", formatInfo.ShortDatePattern, formatInfo.ShortTimePattern), formatInfo)); case DateTimeFormatPattern.ShortTime: return(value.ToString(formatInfo.ShortTimePattern, formatInfo)); default: throw new ArgumentOutOfRangeException(nameof(pattern)); } }
/// <summary> /// Returns a string expression representing a standardized date- and time value. /// </summary> /// <param name="value">The <see cref="DateTime"/> value to be formatted.</param> /// <param name="pattern">The standardized patterns to apply on <paramref name="value"/>.</param> /// <returns>Returns a string expression representing a date- and time value.</returns> public static string FromDateTime(DateTime value, DateTimeFormatPattern pattern) { return(FromDateTime(value, pattern, CultureInfo.InvariantCulture)); }