/// <summary> /// Returns a <see cref="System.String" /> that represents this instance. /// </summary> /// <param name="pattern">The date format pattern.</param> /// <param name="date">The date to format.</param> /// <returns> /// A <see cref="System.String" /> that represents this instance. /// </returns> public static string ToString(string pattern, ScriptDate date) { if (string.IsNullOrEmpty(pattern)) { return(date.ToString()); } // If pattern is %g only, use the default date if (pattern == "%g") { return(ToString("%g %d %b %Y", date)); } var builder = new StringBuilder(); var defaultCulture = CultureInfo.CurrentCulture; for (int i = 0; i < pattern.Length; i++) { var c = pattern[i]; if (c == '%' && (i + 1) < pattern.Length) { i++; Func <DateTime, CultureInfo, string> formatter; var format = pattern[i]; // Switch to invariant culture if (format == 'g') { defaultCulture = CultureInfo.InvariantCulture; continue; } if (Formats.TryGetValue(format, out formatter)) { builder.Append(formatter.Invoke(date, defaultCulture)); } else { builder.Append('%'); builder.Append(format); } } else { builder.Append(c); } } return(builder.ToString()); }
/// <summary> /// Returns a <see cref="System.String" /> that represents this instance. /// </summary> /// <param name="pattern">The date format pattern.</param> /// <param name="date">The date to format.</param> /// <returns> /// A <see cref="System.String" /> that represents this instance. /// </returns> public static string ToString(string pattern, ScriptDate date) { if (string.IsNullOrEmpty(pattern)) { return date.ToString(); } // If pattern is %g only, use the default date if (pattern == "%g") { return ToString("%g %d %b %Y", date); } var builder = new StringBuilder(); var defaultCulture = CultureInfo.CurrentCulture; for (int i = 0; i < pattern.Length; i++) { var c = pattern[i]; if (c == '%' && (i + 1) < pattern.Length) { i++; Func<DateTime, CultureInfo, string> formatter; var format = pattern[i]; // Switch to invariant culture if (format == 'g') { defaultCulture = CultureInfo.InvariantCulture; continue; } if (Formats.TryGetValue(format, out formatter)) { builder.Append(formatter.Invoke(date, defaultCulture)); } else { builder.Append('%'); builder.Append(format); } } else { builder.Append(c); } } return builder.ToString(); }