public static void Main() { string[] myDateTimePatterns = new string[] { "MM/dd/yy", "MM/dd/yyyy" }; // Get the en-US culture. CultureInfo ci = new CultureInfo("en-US"); // Get the DateTimeFormatInfo for the en-US culture. DateTimeFormatInfo dtfi = ci.DateTimeFormat; // Display the effective culture. Console.WriteLine("This code example uses the {0} culture.", ci.Name); // Display the native calendar name. Console.WriteLine("\nNativeCalendarName..."); Console.WriteLine("\"{0}\"", dtfi.NativeCalendarName); // Display month genitive names. Console.WriteLine("\nMonthGenitiveNames..."); foreach (string name in dtfi.MonthGenitiveNames) { Console.WriteLine("\"{0}\"", name); } // Display abbreviated month genitive names. Console.WriteLine("\nAbbreviatedMonthGenitiveNames..."); foreach (string name in dtfi.AbbreviatedMonthGenitiveNames) { Console.WriteLine("\"{0}\"", name); } // Display shortest day names. Console.WriteLine("\nShortestDayNames..."); foreach (string name in dtfi.ShortestDayNames) { Console.WriteLine("\"{0}\"", name); } // Display shortest day name for a particular day of the week. Console.WriteLine("\nGetShortestDayName(DayOfWeek.Sunday)..."); Console.WriteLine("\"{0}\"", dtfi.GetShortestDayName(DayOfWeek.Sunday)); // Display the initial DateTime format patterns for the 'd' format specifier. Console.WriteLine("\nInitial DateTime format patterns for the 'd' format specifier..."); foreach (string name in dtfi.GetAllDateTimePatterns('d')) { Console.WriteLine("\"{0}\"", name); } // Change the initial DateTime format patterns for the 'd' DateTime format specifier. Console.WriteLine("\nChange the initial DateTime format patterns for the \n" + "'d' format specifier to my format patterns..."); dtfi.SetAllDateTimePatterns(myDateTimePatterns, 'd'); // Display the new DateTime format patterns for the 'd' format specifier. Console.WriteLine("\nNew DateTime format patterns for the 'd' format specifier..."); foreach (string name in dtfi.GetAllDateTimePatterns('d')) { Console.WriteLine("\"{0}\"", name); } }
public void GetAllDateTimePatterns_ret_diff_obj() { // We need to return different objects for security reasons DateTimeFormatInfo dtfi = CultureInfo.InvariantCulture.DateTimeFormat; string [] one = dtfi.GetAllDateTimePatterns(); string [] two = dtfi.GetAllDateTimePatterns(); Assert.IsTrue(one != two); }
internal static String[] GetAllDateTimes(DateTime dateTime, char format, DateTimeFormatInfo dtfi) { String [] allFormats = null; String [] results = null; switch (format) { case 'd': case 'D': case 'f': case 'F': case 'g': case 'G': case 'm': case 'M': case 't': case 'T': case 'y': case 'Y': allFormats = dtfi.GetAllDateTimePatterns(format); results = new String[allFormats.Length]; for (int i = 0; i < allFormats.Length; i++) { results[i] = Format(dateTime, allFormats[i], dtfi); } break; case 'U': DateTime universalTime = dateTime.ToUniversalTime(); allFormats = dtfi.GetAllDateTimePatterns(format); results = new String[allFormats.Length]; for (int i = 0; i < allFormats.Length; i++) { results[i] = Format(universalTime, allFormats[i], dtfi); } break; // // The following ones are special cases because these patterns are read-only in // DateTimeFormatInfo. // case 'r': case 'R': case 's': case 'u': results = new String[] { Format(dateTime, new String(new char[] { format }), dtfi) }; break; default: throw new FormatException(Environment.GetResourceString("Format_InvalidString")); } return(results); }
internal static string[] GetAllDateTimes(DateTime dateTime, char format, DateTimeFormatInfo dtfi) { string[] allDateTimePatterns = null; string[] strArray2 = null; switch (format) { case 'D': case 'F': case 'G': case 'M': case 'T': case 'd': case 'f': case 'g': case 'Y': case 'm': case 't': case 'y': allDateTimePatterns = dtfi.GetAllDateTimePatterns(format); strArray2 = new string[allDateTimePatterns.Length]; for (int i = 0; i < allDateTimePatterns.Length; i++) { strArray2[i] = Format(dateTime, allDateTimePatterns[i], dtfi); } return(strArray2); case 'O': case 'R': case 'o': case 'r': case 's': case 'u': return(new string[] { Format(dateTime, new string(new char[] { format }), dtfi) }); case 'U': { DateTime time = dateTime.ToUniversalTime(); allDateTimePatterns = dtfi.GetAllDateTimePatterns(format); strArray2 = new string[allDateTimePatterns.Length]; for (int j = 0; j < allDateTimePatterns.Length; j++) { strArray2[j] = Format(time, allDateTimePatterns[j], dtfi); } return(strArray2); } } throw new FormatException(Environment.GetResourceString("Format_InvalidString")); }
static void AnotherTest(string[] args) { UtcDate ud = new UtcDate { EventTime = new DateTime(2009, 12, 24, 8, 30, 0, DateTimeKind.Utc) }; string timeFmt = "HH':'mm':'ss.fffffffK"; XmlSerializer xs = new XmlSerializer(typeof(UtcDate)); xs.Serialize(Console.Out, ud); Console.WriteLine(); DateTime d = DateTime.Parse("12:30:00Z"); Console.WriteLine($"{d:u} {d.Kind} {d.ToString(timeFmt)}"); d = DateTime.SpecifyKind(d, DateTimeKind.Utc); Console.WriteLine($"{d:u} {d.Kind} {d.ToString(timeFmt)}"); d = DateTime.SpecifyKind(d, DateTimeKind.Unspecified); Console.WriteLine($"{d:u} {d.Kind} {d.ToString(timeFmt)}"); Console.WriteLine(DateTimeFormatInfo.InvariantInfo.LongTimePattern + " " + d.ToString(DateTimeFormatInfo.InvariantInfo.LongTimePattern, CultureInfo.InvariantCulture)); DateTimeFormatInfo invDTF = new DateTimeFormatInfo(); String[] formats = invDTF.GetAllDateTimePatterns(); Console.WriteLine("{0,-40} {1}\n", "Pattern", "Result String"); foreach (var fmt in formats) { Console.WriteLine("{0,-40} {1}", fmt, d.ToString(fmt)); } }
public static bool ValidateMachineDateTimeFromat() { CultureInfo ci = CultureInfo.CurrentCulture; DateTimeFormatInfo dtfi = ci.DateTimeFormat; string[] SystemDateTimePatterns = new string[250]; int i = 0; foreach (string name in dtfi.GetAllDateTimePatterns('d')) { SystemDateTimePatterns[i] = name; i++; } string[] myDateTimeFormat = { "dd/MM/yyyy" }; if (!myDateTimeFormat[0].Equals(SystemDateTimePatterns[0])) { MessageBox.Show("Your Machine DateTime Format is: " + SystemDateTimePatterns[0] + ".\n" + "Required DateTime Format is: dd/MM/yyyy. \nPlease Change the Datetime Format.", "System Datetime Format", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } else { return(true); } }
protected void lstLANGUAGE_Changed(Object sender, EventArgs e) { if (lstLANGUAGE.SelectedValue.Length > 0) { CultureInfo oldCulture = Thread.CurrentThread.CurrentCulture; CultureInfo oldUICulture = Thread.CurrentThread.CurrentUICulture; Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lstLANGUAGE.SelectedValue); Thread.CurrentThread.CurrentUICulture = new CultureInfo(lstLANGUAGE.SelectedValue); DateTime dtNow = T10n.FromServerTime(DateTime.Now); DateTimeFormatInfo oDateInfo = Thread.CurrentThread.CurrentCulture.DateTimeFormat; NumberFormatInfo oNumberInfo = Thread.CurrentThread.CurrentCulture.NumberFormat; String[] aDateTimePatterns = oDateInfo.GetAllDateTimePatterns(); lstDATE_FORMAT.Items.Clear(); lstTIME_FORMAT.Items.Clear(); foreach (string sPattern in aDateTimePatterns) { // 11/12/2005 Paul. Only allow patterns that have a full year. if (sPattern.IndexOf("yyyy") >= 0 && sPattern.IndexOf("dd") >= 0 && sPattern.IndexOf("mm") < 0) { lstDATE_FORMAT.Items.Add(new ListItem(sPattern + " " + dtNow.ToString(sPattern), sPattern)); } if (sPattern.IndexOf("yy") < 0 && sPattern.IndexOf("mm") >= 0) { lstTIME_FORMAT.Items.Add(new ListItem(sPattern + " " + dtNow.ToString(sPattern), sPattern)); } } Thread.CurrentThread.CurrentCulture = oldCulture; Thread.CurrentThread.CurrentCulture = oldUICulture; } }
private void setSystemDatePattern() { CultureInfo ci = CultureInfo.CurrentCulture; DateTimeFormatInfo dtfi = ci.DateTimeFormat; string[] SystemDateTimePatterns = new string[250]; int o = 0; foreach (string name in dtfi.GetAllDateTimePatterns('d')) { SystemDateTimePatterns[o] = name; o++; } string[] myDateTimeFormat = { "dd/MM/yyyy", "dd/MM/yyyy" }; if (myDateTimeFormat[0].Equals(SystemDateTimePatterns[0]) || myDateTimeFormat[1].Equals(SystemDateTimePatterns[0])) { } else { RegistryKey rkey = Registry.CurrentUser.OpenSubKey(@"Control Panel\International", true); rkey.SetValue("sShortDate", "dd/MM/yyyy"); rkey.SetValue("sLongDate", "dd/MM/yyyy"); Application.Restart(); //MessageBox.Show("Your System DateTime Format is: " + SystemDateTimePatterns[0] + "\n" + "Required DateTime Format: dd/MM/yyyy"); //Application.Exit(); } }
public String[] GetDateTimeFormats() { DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null); return(GetDateTimeFormats(info, info.GetAllDateTimePatterns())); }
public String[] GetDateTimeFormats(IFormatProvider provider) { DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(provider); return(GetDateTimeFormats(info, info.GetAllDateTimePatterns())); }
public static void Main() { Console.WriteLine("Hello!"); var now = DateTime.Now; Console.WriteLine(DateTime.Now); var parsed = DateTime.Parse(now.ToString()); if (now.ToString() != parsed.ToString()) { throw new AssertionException(); } var dtfi = new DateTimeFormatInfo(); Console.WriteLine($"DTFI: |{dtfi.DateSeparator}|{dtfi.TimeSeparator}| - {dtfi.DateSeparator == dtfi.TimeSeparator}"); foreach (var pattern in dtfi.GetAllDateTimePatterns('y')) { Console.WriteLine($"DTFI PATTERN: |{pattern}|"); } Console.WriteLine(dtfi.IsReadOnly); }
public static void Main() { // Creates a new DateTimeFormatinfo. DateTimeFormatInfo myDtfi = new DateTimeFormatInfo(); // Gets and prints all the patterns. String[] myPatternsArray = myDtfi.GetAllDateTimePatterns(); Console.WriteLine("ALL the patterns:"); PrintIndexAndValues(myPatternsArray); // Gets and prints the pattern(s) associated with some of the format characters. myPatternsArray = myDtfi.GetAllDateTimePatterns('d'); Console.WriteLine("The patterns for 'd':"); PrintIndexAndValues(myPatternsArray); myPatternsArray = myDtfi.GetAllDateTimePatterns('D'); Console.WriteLine("The patterns for 'D':"); PrintIndexAndValues(myPatternsArray); myPatternsArray = myDtfi.GetAllDateTimePatterns('f'); Console.WriteLine("The patterns for 'f':"); PrintIndexAndValues(myPatternsArray); myPatternsArray = myDtfi.GetAllDateTimePatterns('F'); Console.WriteLine("The patterns for 'F':"); PrintIndexAndValues(myPatternsArray); myPatternsArray = myDtfi.GetAllDateTimePatterns('r'); Console.WriteLine("The patterns for 'r':"); PrintIndexAndValues(myPatternsArray); myPatternsArray = myDtfi.GetAllDateTimePatterns('R'); Console.WriteLine("The patterns for 'R':"); PrintIndexAndValues(myPatternsArray); }
public static bool TryParseDateTime(string strDateTime, CultureInfo formatProvider, out DateTimeOffset dateTimeOffset, out bool hasTimeOffset) { hasTimeOffset = false; if (DateTimeOffset.TryParse(strDateTime, (IFormatProvider)formatProvider, DateTimeStyles.None, out dateTimeOffset)) { TimeSpan timeSpan = default(TimeSpan); if (TimeSpan.TryParse(strDateTime, out timeSpan)) { return(false); } DateTimeOffset dateTimeOffset2 = default(DateTimeOffset); if (!DateTimeOffset.TryParse(strDateTime + " +0", (IFormatProvider)formatProvider, DateTimeStyles.None, out dateTimeOffset2)) { hasTimeOffset = true; } return(true); } DateTimeFormatInfo dateTimeFormatInfo = (formatProvider != null) ? formatProvider.DateTimeFormat : CultureInfo.CurrentCulture.DateTimeFormat; string[] allDateTimePatterns = dateTimeFormatInfo.GetAllDateTimePatterns('d'); if (!DateTimeOffset.TryParseExact(strDateTime, allDateTimePatterns, (IFormatProvider)formatProvider, DateTimeStyles.None, out dateTimeOffset)) { string[] allDateTimePatterns2 = dateTimeFormatInfo.GetAllDateTimePatterns('G'); if (!DateTimeOffset.TryParseExact(strDateTime, allDateTimePatterns2, (IFormatProvider)formatProvider, DateTimeStyles.None, out dateTimeOffset)) { for (int i = 0; i < allDateTimePatterns2.Length; i++) { string[] array; string[] array2 = array = allDateTimePatterns2; int num = i; IntPtr intPtr = (IntPtr)num; array2[num] = array[(long)intPtr] + " zzz"; } if (!DateTimeOffset.TryParseExact(strDateTime, allDateTimePatterns2, (IFormatProvider)formatProvider, DateTimeStyles.None, out dateTimeOffset)) { return(false); } hasTimeOffset = true; } } return(true); }
static void Main() { //needed to display cyrillic characters Console.OutputEncoding = Encoding.Unicode; DateTimeFormatInfo di = DateTimeFormatInfo.CurrentInfo; string[] dateTimeFormats = di.GetAllDateTimePatterns(); PrintDateTimeFormats(dateTimeFormats); }
static void f(DateTime value) { DateTimeFormatInfo dfi = DateTimeFormatInfo.InvariantInfo; string[] pats = dfi.GetAllDateTimePatterns(); for (int i = 0; i < pats.Length; ++i) { string s = _ToString(pats[i], dfi, value); Console.WriteLine(s); } }
public static void Main() { DateTime date = new DateTime(2014, 8, 28, 12, 28, 30); DateTimeFormatInfo invDTF = new DateTimeFormatInfo(); String[] formats = invDTF.GetAllDateTimePatterns(); Console.WriteLine("{0,-40} {1}\n", "Pattern", "Result String"); foreach (var fmt in formats) { Console.WriteLine("{0,-40} {1}", fmt, date.ToString(fmt)); } }
public void AllDateTimePatternsTest(string cultureName) { char[] formats = { 'd', 'D', 'f', 'F', 'g', 'G', 'm', 'o', 'r', 's', 't', 'T', 'u', 'U', 'y' }; DateTimeFormatInfo dtfi = (DateTimeFormatInfo) new CultureInfo(cultureName).DateTimeFormat.Clone(); var allPatterns = dtfi.GetAllDateTimePatterns(); Dictionary <string, string> dic = new Dictionary <string, string>(); string value = ""; foreach (char format in formats) { foreach (string pattern in dtfi.GetAllDateTimePatterns(format)) { if (!dic.TryGetValue(pattern, out value)) { dic.Add(pattern, ""); } } } foreach (string pattern in allPatterns) { Assert.True(dic.TryGetValue(pattern, out value), "Couldn't find the pattern in the patterns list"); } char[] setterFormats = { 'd', 'D', 't', 'T', 'y', 'Y' }; foreach (char format in setterFormats) { var formatPatterns = dtfi.GetAllDateTimePatterns(format); string [] newPatterns = new string[1] { formatPatterns[formatPatterns.Length - 1] }; dtfi.SetAllDateTimePatterns(newPatterns, format); Assert.Equal(newPatterns, dtfi.GetAllDateTimePatterns(format)); } }
internal static bool TryParseDateTime(string strDateTime, CultureInfo formatProvider, out DateTimeOffset dateTimeOffset, out bool hasTimeOffset) { hasTimeOffset = false; if (DateTimeOffset.TryParse(strDateTime, formatProvider, DateTimeStyles.None, out dateTimeOffset)) { if (TimeSpan.TryParse(strDateTime, out TimeSpan _)) { return(false); } if (!DateTimeOffset.TryParse(strDateTime + " +0", formatProvider, DateTimeStyles.None, out DateTimeOffset _)) { hasTimeOffset = true; } return(true); } DateTimeFormatInfo dateTimeFormatInfo = (formatProvider != null) ? formatProvider.DateTimeFormat : CultureInfo.CurrentCulture.DateTimeFormat; string[] allDateTimePatterns = dateTimeFormatInfo.GetAllDateTimePatterns('d'); if (!DateTimeOffset.TryParseExact(strDateTime, allDateTimePatterns, formatProvider, DateTimeStyles.None, out dateTimeOffset)) { string[] allDateTimePatterns2 = dateTimeFormatInfo.GetAllDateTimePatterns('G'); if (!DateTimeOffset.TryParseExact(strDateTime, allDateTimePatterns2, formatProvider, DateTimeStyles.None, out dateTimeOffset)) { for (int i = 0; i < allDateTimePatterns2.Length; i++) { allDateTimePatterns2[i] += " zzz"; } if (!DateTimeOffset.TryParseExact(strDateTime, allDateTimePatterns2, formatProvider, DateTimeStyles.None, out dateTimeOffset)) { return(false); } hasTimeOffset = true; } } return(true); }
//////////////////////////////////////////////////////////////////////////// // // Given a DTFI, get all of the date words from date patterns and time patterns. // //////////////////////////////////////////////////////////////////////////// internal String[] GetDateWordsOfDTFI(DateTimeFormatInfo dtfi) { // Enumarate all LongDatePatterns, and get the DateWords and scan for month postfix. String[] datePatterns = dtfi.GetAllDateTimePatterns('D'); int i; // Scan the long date patterns for (i = 0; i < datePatterns.Length; i++) { ScanDateWord(datePatterns[i]); } // Scan the short date patterns datePatterns = dtfi.GetAllDateTimePatterns('d'); for (i = 0; i < datePatterns.Length; i++) { ScanDateWord(datePatterns[i]); } // Scan the YearMonth patterns. datePatterns = dtfi.GetAllDateTimePatterns('y'); for (i = 0; i < datePatterns.Length; i++) { ScanDateWord(datePatterns[i]); } // Scan the month/day pattern ScanDateWord(dtfi.MonthDayPattern); // Scan the long time patterns. datePatterns = dtfi.GetAllDateTimePatterns('T'); for (i = 0; i < datePatterns.Length; i++) { ScanDateWord(datePatterns[i]); } // Scan the short time patterns. datePatterns = dtfi.GetAllDateTimePatterns('t'); for (i = 0; i < datePatterns.Length; i++) { ScanDateWord(datePatterns[i]); } String[] result = null; if (m_dateWords != null && m_dateWords.Count > 0) { result = new String[m_dateWords.Count]; for (i = 0; i < m_dateWords.Count; i++) { result[i] = m_dateWords[i]; } } return (result); }
internal static bool ValidateFormat(DateTimeFormatInfo dateTimeFormatInfo, string format, char dateTimePatternSelector, out string[] validFormats) { validFormats = dateTimeFormatInfo.GetAllDateTimePatterns(dateTimePatternSelector); return(Array.Exists <string>(validFormats, (string value) => value == format.Trim())); }
// Token: 0x060015F1 RID: 5617 RVA: 0x000414C4 File Offset: 0x0003F6C4 internal static string[] GetAllDateTimes(DateTime dateTime, char format, DateTimeFormatInfo dtfi) { string[] allDateTimePatterns; string[] array; if (format <= 'U') { switch (format) { case 'D': case 'F': case 'G': break; case 'E': goto IL_140; default: switch (format) { case 'M': case 'T': break; case 'N': case 'P': case 'Q': case 'S': goto IL_140; case 'O': case 'R': goto IL_11E; case 'U': { DateTime dateTime2 = dateTime.ToUniversalTime(); allDateTimePatterns = dtfi.GetAllDateTimePatterns(format); array = new string[allDateTimePatterns.Length]; for (int i = 0; i < allDateTimePatterns.Length; i++) { array[i] = DateTimeFormat.Format(dateTime2, allDateTimePatterns[i], dtfi); } return(array); } default: goto IL_140; } break; } } else if (format != 'Y') { switch (format) { case 'd': case 'f': case 'g': break; case 'e': goto IL_140; default: switch (format) { case 'm': case 't': case 'y': break; case 'n': case 'p': case 'q': case 'v': case 'w': case 'x': goto IL_140; case 'o': case 'r': case 's': case 'u': goto IL_11E; default: goto IL_140; } break; } } allDateTimePatterns = dtfi.GetAllDateTimePatterns(format); array = new string[allDateTimePatterns.Length]; for (int j = 0; j < allDateTimePatterns.Length; j++) { array[j] = DateTimeFormat.Format(dateTime, allDateTimePatterns[j], dtfi); } return(array); IL_11E: return(new string[] { DateTimeFormat.Format(dateTime, new string(new char[] { format }), dtfi) }); IL_140: throw new FormatException(Environment.GetResourceString("Format_InvalidString")); }