public static string MSUtc(string dateTime) { XsdDateTime xdt; DateTime dt; try { if (!XsdDateTime.TryParse(dateTime, XsdDateTimeFlags.AllXsd | XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrTimeNoTz, out xdt)) { return(string.Empty); } dt = xdt.ToZulu(); } catch (ArgumentException) { // Operations with DateTime can throw this exception eventualy return(string.Empty); } char[] text = "----------T00:00:00.000".ToCharArray(); // "YYYY-MM-DDTHH:NN:SS.III" // 0 1 2 // 01234567890123456789012 switch (xdt.TypeCode) { case XmlTypeCode.DateTime: PrintDate(text, dt); PrintTime(text, dt); break; case XmlTypeCode.Time: PrintTime(text, dt); break; case XmlTypeCode.Date: PrintDate(text, dt); break; case XmlTypeCode.GYearMonth: PrintYear(text, dt.Year); ShortToCharArray(text, 5, dt.Month); break; case XmlTypeCode.GYear: PrintYear(text, dt.Year); break; case XmlTypeCode.GMonthDay: ShortToCharArray(text, 5, dt.Month); ShortToCharArray(text, 8, dt.Day); break; case XmlTypeCode.GDay: ShortToCharArray(text, 8, dt.Day); break; case XmlTypeCode.GMonth: ShortToCharArray(text, 5, dt.Month); break; } return(new string(text)); }
public static string MSFormatDateTime(string dateTime, string format, string lang, bool isDate) { try { string locale = GetCultureInfo(lang).Name; XsdDateTime xdt; if (!XsdDateTime.TryParse(dateTime, XsdDateTimeFlags.AllXsd | XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrTimeNoTz, out xdt)) { return(string.Empty); } DateTime dt = xdt.ToZulu(); // If format is the empty string or not specified, use the default format for the given locale if (format.Length == 0) { format = null; } return(dt.ToString(format, new CultureInfo(locale))); } catch (ArgumentException) { // Operations with DateTime can throw this exception eventualy return(string.Empty); } }
// string ms:format-date(string datetime[, string format[, string language]]) // string ms:format-time(string datetime[, string format[, string language]]) // // Format xsd:dateTime as a date/time string for a given language using a given format string. // * Datetime contains a lexical representation of xsd:dateTime. If datetime is not valid, the // empty string is returned. // * Format specifies a format string in the same way as for GetDateFormat/GetTimeFormat system // functions. If format is the empty string or not passed, the default date/time format for the // given culture is used. // * Language specifies a culture used for formatting. If language is the empty string or not // passed, the current culture is used. If language is not recognized, a runtime error happens. public static string MSFormatDateTime(string dateTime, string format, string lang, bool isDate) { try { int locale = GetCultureInfo(lang).LCID; XsdDateTime xdt; if (!XsdDateTime.TryParse(dateTime, XsdDateTimeFlags.AllXsd | XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrTimeNoTz, out xdt)) { return(string.Empty); } SystemTime st = new SystemTime(xdt.ToZulu()); StringBuilder sb = new StringBuilder(format.Length + 16); // If format is the empty string or not specified, use the default format for the given locale if (format.Length == 0) { format = null; } if (isDate) { int res = GetDateFormat(locale, 0, ref st, format, sb, sb.Capacity); if (res == 0) { res = GetDateFormat(locale, 0, ref st, format, sb, 0); if (res != 0) { sb = new StringBuilder(res); res = GetDateFormat(locale, 0, ref st, format, sb, sb.Capacity); } } } else { int res = GetTimeFormat(locale, 0, ref st, format, sb, sb.Capacity); if (res == 0) { res = GetTimeFormat(locale, 0, ref st, format, sb, 0); if (res != 0) { sb = new StringBuilder(res); res = GetTimeFormat(locale, 0, ref st, format, sb, sb.Capacity); } } } return(sb.ToString()); } catch (ArgumentException) { // Operations with DateTime can throw this exception eventualy return(string.Empty); } }