Bear in mind that a DateTime should generally be stored in Universal format and converted to local format when be displayed in a GUI or used in a report. This handy extension for date time format strings saves having to remember their obscure format codes or look them up every time.
// See here for more info // https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings public static class DateTimeToStringExtender { public static bool ParseGeneralShortDateTime(this string dateTimeStr, out DateTime dateTime) { var format = "g"; // Standard short date time format: "dd/MM/yyyy HH:mm:ss" for UK bool result = false; CultureInfo provider = CultureInfo.CurrentCulture; result = DateTime.TryParseExact(dateTimeStr.Trim(), format, provider, DateTimeStyles.None, out dateTime); return result; } /// <summary> /// Convert a DateTime to a YearMonthDay string of the form yyyy/MM/dd /// </summary> /// <param name="dt">target</param> /// <returns>YearMonthDay string of the form yyyyMMdd</returns> public static string ToYyyyMmDdString(this DateTime dt, char separator = char.MinValue) { string format = "yyyyMMdd"; if (!char.IsControl(separator)) { format = "yyyy" + separator + "MM" + separator + "dd"; } var res = dt.ToString(format); return res; } /// <summary> /// Convert a DateTime to a YearMonthDay string of the form DdMmYyyy /// </summary> /// <param name="dt">target</param> /// <returns>YearMonthDay string of the form DdMmYyyy</returns> public static string ToDdMmYyyyString(this DateTime dt, char separator = char.MinValue) { string format = "ddMMyyyy"; if (!char.IsControl(separator)) { format = "dd" + separator + "MM" + separator + "yyyy"; } return dt.ToString(format); } /// <summary> /// Convert a DateTime to a YearMonthDay string of the form yyyyMMdd_HHmmss where the hour is in the 24 hour format /// </summary> /// <param name="dt">target</param> /// <returns>YearMonthDay string of the form yyyyMMdd_HHmmss</returns> public static string ToYyyyMmDd_HhMmSsString(this DateTime dt) { return dt.ToString("yyyyMMdd_HHmmss"); } /// <summary> /// Convert a DateTime to a YearMonthDay string of the form yyyyMMdd_HHmmss_ffffff where the hour is in the 24 hour format /// Useful for creating randomised filenames based upon the date /// </summary> /// <param name="dt">target</param> /// <returns>YearMonthDay string of the form yyyyMMdd_HHmmss_ffffff</returns> public static string To_YyyyMmDd_HhMmSs_ffffff(this DateTime dt) { return dt.ToString("yyyyMMdd_HHmmss_ffffff"); } /// <summary> /// Convert a DateTime to a YearMonthDay string of the form yyyyMMdd_HHmmss where the hour is in the 24 hour format /// </summary> /// <param name="dt">target</param> /// <returns>YearMonthDay string of the form yyyyMMdd_HHmmss</returns> public static string ToYyyyMmDd_HhMmSsString(this DateTime dt, char dateSeparator = char.MinValue, char timeSeparator = char.MinValue) { string format = "yyyyMMdd_HHmmss"; if (!char.IsLetterOrDigit(dateSeparator) && !char.IsControl(dateSeparator)) { format = "yyyy" + dateSeparator + "MM" + dateSeparator + "dd" + "_" + "HH" + timeSeparator + "mm" + timeSeparator + "ss"; } return dt.ToString(format); } /// <summary> /// Emits a date time (culture independent) string of the form /// "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'" /// (eg.: "Sun, 09 Mar 2008 16:05:07 GMT"). /// Works on universal date times only /// </summary> /// <param name="dateTime">target date time to convert to a string</param> /// <returns>dateTime formatted as a string</returns> public static string ToRfc1123FormatString(this DateTime dateTime) { string result = dateTime.ToUniversalTime().ToString("r"); return result; } /// <summary> /// Convert a DateTime to a string of the form "2008-03-09 16:05:07Z" /// where the '-' character is culture dependant /// </summary> /// <param name="dt">target</param> /// <returns>string of the form "2008-03-09 16:05:07Z" where '-' character is culture dependant</returns> public static string ToUniversalSortableString(this DateTime dt) { return dt.ToUniversalTime().ToString("u"); } // 2009-06-15T13:45:30 --> Monday, June 15, 2009 8:45:30 PM (for en-US) public static string ToUniversalFull(this DateTime dateTime) { var res = dateTime.ToUniversalTime().ToString("U"); return res; } /// <summary> /// Emits a sortable date time (culture independent) string of the form "yyyy'-'MM'-'dd'T'HH':'mm':'ss" (eg.: "2008-03-09T16:05:07"") /// </summary> /// <param name="dateTime">target date time to convert to a string</param> /// <returns>dateTime formatted as a string</returns> public static string ToSortableFormatString(this DateTime dateTime) { string result = dateTime.ToString("s"); return result; } /// <summary> /// Emits a result date time format string using a pattern that preserves /// time zone information and emits a result string that complies with /// ISO 8601 ("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK"), preserves /// the DateTime kind. /// </summary> /// <param name="dateTime">target date time to convert to a string</param> /// <returns>dateTime formatted as a string</returns> // 2009-06-15T13:45:30 (DateTimeKind.Utc) -->; 2009-06-15T13:45:30.0000000Z // 2009-06-15T13:45:30 (DateTimeKind.Local) --> 2009-06-15T13:45:30.0000000-07:00 (depending on the time zone) // 2009-06-15T13:45:30 (DateTimeKind.Unspecified) --> 2009-06-15T13:45:30.0000000 public static string ToRoundTripFormatString(this DateTime dateTime) { string result = dateTime.ToString("o"); return result; } static readonly string[] formats = { // Extended formats "o", "yyyy-MM-ddTHH:mm:ss.ffffffZ", "yyyy-MM-ddTHH:mm:ss.fffffZ", "yyyy-MM-ddTHH:mm:ss.ffffZ", "yyyy-MM-ddTHH:mm:ss.fffZ", "yyyy-MM-ddTHH:mm:ss.ffZ", "yyyy-MM-ddTHH:mm:ss.fZ", "yyyy-MM-ddTHH:mm:ssZ" }; public static DateTime ParseToIso8601DateTime(this string str) { return DateTime.ParseExact(str, formats, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind); } }
No comments:
Post a Comment