/// <summary> /// <para>Specifies a custom format to use.</para> /// <para>See https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings for more details.</para> /// </summary> /// <param name="format">Custom format string to use.</param> public DateTimeFormatAttribute(string format) { if (string.IsNullOrWhiteSpace(format)) { throw new ArgumentNullException(nameof(format), "Specified format cannot be null or empty."); } this.Kind = DateTimeFormatKind.Custom; this.Format = format; }
/// <summary> /// Specifies a predefined format to use. /// </summary> /// <param name="kind">Predefined format kind to use.</param> public DateTimeFormatAttribute(DateTimeFormatKind kind) { if (kind < 0 || kind > DateTimeFormatKind.InvariantLocaleShort) { throw new ArgumentOutOfRangeException(nameof(kind), "Specified format kind is not legal or supported."); } this.Kind = kind; this.Format = null; }
/// <summary> /// Gets the format string associated with a specified <see cref="DateTimeFormatKind"/>. /// </summary> /// <param name="formatKind">The format kind.</param> /// <returns> /// The format string associated with a specified <see cref="DateTimeFormatKind"/>. /// </returns> public static string ToFormatString( this DateTimeFormatKind formatKind) { if (formatKind == DateTimeFormatKind.Unknown) { throw new ArgumentOutOfRangeException(nameof(formatKind), Invariant($"{nameof(formatKind)} is {nameof(DateTimeFormatKind)}.{nameof(DateTimeFormatKind.Unknown)}")); } if (!DateTimeFormatKindToFormatStringMap.ContainsKey(formatKind)) { throw new NotSupportedException(Invariant($"This {nameof(DateTimeFormatKind)} is not supported: {formatKind}.")); } var result = DateTimeFormatKindToFormatStringMap[formatKind]; return(result); }
/// <summary> /// Converts the value of the current <see cref="DateTime"/> object to its equivalent /// string representation using the specified format and culture-specific format information. /// </summary> /// <param name="value">The value.</param> /// <param name="formatKind">The kind of formatting to apply.</param> /// <param name="cultureKind">OPTIONAL kind of culture to use. DEFAULT is to use the invariant culture.</param> /// <returns> /// A string representation of the specified <see cref="DateTime"/> value as specified by format and culture. /// </returns> public static string ToString( this DateTime value, DateTimeFormatKind formatKind, CultureKind cultureKind = CultureKind.Invariant) { if (formatKind == DateTimeFormatKind.Unknown) { throw new ArgumentOutOfRangeException(nameof(formatKind), Invariant($"{nameof(formatKind)} is {nameof(DateTimeFormatKind)}.{nameof(DateTimeFormatKind.Unknown)}")); } if (cultureKind == CultureKind.Unknown) { throw new ArgumentOutOfRangeException(nameof(cultureKind), Invariant($"{nameof(cultureKind)} is {nameof(CultureKind)}.{nameof(CultureKind.Unknown)}")); } var formatString = formatKind.ToFormatString(); var cultureInfo = cultureKind.ToCultureInfo(); var result = value.ToString(formatString, cultureInfo); return(result); }