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); } }
///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToDateTime3"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public static DateTime ToDateTime(string s, XmlDateTimeSerializationMode dateTimeOption) { XsdDateTime xsdDateTime = new XsdDateTime(s, XsdDateTimeFlags.AllXsd); DateTime dt = (DateTime)xsdDateTime; switch (dateTimeOption) { case XmlDateTimeSerializationMode.Local: dt = SwitchToLocalTime(dt); break; case XmlDateTimeSerializationMode.Utc: dt = SwitchToUtcTime(dt); break; case XmlDateTimeSerializationMode.Unspecified: dt = new DateTime(dt.Ticks, DateTimeKind.Unspecified); break; case XmlDateTimeSerializationMode.RoundtripKind: break; default: throw new ArgumentException(SR.Format(SR.Sch_InvalidDateTimeOption, dateTimeOption, "dateTimeOption")); } return(dt); }
///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString14"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public static string ToString(DateTime value, XmlDateTimeSerializationMode dateTimeOption) { switch (dateTimeOption) { case XmlDateTimeSerializationMode.Local: value = SwitchToLocalTime(value); break; case XmlDateTimeSerializationMode.Utc: value = SwitchToUtcTime(value); break; case XmlDateTimeSerializationMode.Unspecified: value = new DateTime(value.Ticks, DateTimeKind.Unspecified); break; case XmlDateTimeSerializationMode.RoundtripKind: break; default: throw new ArgumentException(SR.Format(SR.Sch_InvalidDateTimeOption, dateTimeOption, "dateTimeOption")); } XsdDateTime xsdDateTime = new XsdDateTime(value, XsdDateTimeFlags.DateTime); return(xsdDateTime.ToString()); }
public static DateTimeOffset ToDateTimeOffset(string s) { if (s == null) { throw new ArgumentNullException("s"); } XsdDateTime xsdDateTime = new XsdDateTime(s, XsdDateTimeFlags.AllXsd); DateTimeOffset dateTimeOffset = (DateTimeOffset)xsdDateTime; return(dateTimeOffset); }
// 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); } }
public static string ToString(DateTimeOffset value) { XsdDateTime xsdDateTime = new XsdDateTime(value); return(xsdDateTime.ToString()); }