本文整理汇总了C#中System.Globalization.DateTimeFormatInfo.GetAllDateTimePatterns方法的典型用法代码示例。如果您正苦于以下问题:C# DateTimeFormatInfo.GetAllDateTimePatterns方法的具体用法?C# DateTimeFormatInfo.GetAllDateTimePatterns怎么用?C# DateTimeFormatInfo.GetAllDateTimePatterns使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Globalization.DateTimeFormatInfo
的用法示例。
在下文中一共展示了DateTimeFormatInfo.GetAllDateTimePatterns方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
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));
}
}
输出:
Pattern Result String MM/dd/yyyy 08/28/2014 yyyy-MM-dd 2014-08-28 dddd, dd MMMM yyyy Thursday, 28 August 2014 dddd, dd MMMM yyyy HH:mm Thursday, 28 August 2014 12:28 dddd, dd MMMM yyyy hh:mm tt Thursday, 28 August 2014 12:28 PM dddd, dd MMMM yyyy H:mm Thursday, 28 August 2014 12:28 dddd, dd MMMM yyyy h:mm tt Thursday, 28 August 2014 12:28 PM dddd, dd MMMM yyyy HH:mm:ss Thursday, 28 August 2014 12:28:30 MM/dd/yyyy HH:mm 08/28/2014 12:28 MM/dd/yyyy hh:mm tt 08/28/2014 12:28 PM MM/dd/yyyy H:mm 08/28/2014 12:28 MM/dd/yyyy h:mm tt 08/28/2014 12:28 PM yyyy-MM-dd HH:mm 2014-08-28 12:28 yyyy-MM-dd hh:mm tt 2014-08-28 12:28 PM yyyy-MM-dd H:mm 2014-08-28 12:28 yyyy-MM-dd h:mm tt 2014-08-28 12:28 PM MM/dd/yyyy HH:mm:ss 08/28/2014 12:28:30 yyyy-MM-dd HH:mm:ss 2014-08-28 12:28:30 MMMM dd August 28 MMMM dd August 28 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2014-08-28T12:28:30.0000000 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2014-08-28T12:28:30.0000000 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Thu, 28 Aug 2014 12:28:30 GMT ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Thu, 28 Aug 2014 12:28:30 GMT yyyy'-'MM'-'dd'T'HH':'mm':'ss 2014-08-28T12:28:30 HH:mm 12:28 hh:mm tt 12:28 PM H:mm 12:28 h:mm tt 12:28 PM HH:mm:ss 12:28:30 yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2014-08-28 12:28:30Z dddd, dd MMMM yyyy HH:mm:ss Thursday, 28 August 2014 12:28:30 yyyy MMMM 2014 August yyyy MMMM 2014 August
示例2: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo culture = CultureInfo.CreateSpecificCulture("it-IT");
DateTime date1 = new DateTime(2011, 02, 01, 7, 30, 45, 0);
DateTime date2;
int total = 0;
int noRoundTrip = 0;
foreach (var fmt in culture.DateTimeFormat.GetAllDateTimePatterns()) {
total += 1;
if (! DateTime.TryParse(date1.ToString(fmt), out date2)) {
noRoundTrip++;
Console.WriteLine("Unable to parse {0:" + fmt + "} (format '{1}')",
date1, fmt);
}
}
Console.WriteLine("\nUnable to round-trip {0} of {1} format strings.",
noRoundTrip, total);
}
}
输出:
Unable to parse Tuesday 1 February 2011 7.30 (format 'dddd d MMMM yyyy H.mm') Unable to parse Tuesday 1 February 2011 07.30 (format 'dddd d MMMM yyyy HH.mm') Unable to parse 1-Feb-11 7.30 (format 'd-MMM-yy H.mm') Unable to parse 1-Feb-11 07.30 (format 'd-MMM-yy HH.mm') Unable to parse 1 February 2011 7.30 (format 'd MMMM yyyy H.mm') Unable to parse 1 February 2011 07.30 (format 'd MMMM yyyy HH.mm') Unable to parse Tuesday 1 February 2011 7.30.45 (format 'dddd d MMMM yyyy H.mm.ss') Unable to parse Tuesday 1 February 2011 07.30.45 (format 'dddd d MMMM yyyy HH.mm.ss') Unable to parse 1-Feb-11 7.30.45 (format 'd-MMM-yy H.mm.ss') Unable to parse 1-Feb-11 07.30.45 (format 'd-MMM-yy HH.mm.ss') Unable to parse 1 February 2011 7.30.45 (format 'd MMMM yyyy H.mm.ss') Unable to parse 1 February 2011 07.30.45 (format 'd MMMM yyyy HH.mm.ss') Unable to parse 01/02/2011 7.30 (format 'dd/MM/yyyy H.mm') Unable to parse 01/02/2011 07.30 (format 'dd/MM/yyyy HH.mm') Unable to parse 01/Feb/2011 7.30 (format 'dd/MMM/yyyy H.mm') Unable to parse 01/Feb/2011 07.30 (format 'dd/MMM/yyyy HH.mm') Unable to parse 01/02/11 7.30 (format 'dd/MM/yy H.mm') Unable to parse 01/02/11 07.30 (format 'dd/MM/yy HH.mm') Unable to parse 01.2.11 7.30 (format 'dd.M.yy H.mm') Unable to parse 01.2.11 07.30 (format 'dd.M.yy HH.mm') Unable to parse 1/2/11 7.30 (format 'd/M/yy H.mm') Unable to parse 1/2/11 07.30 (format 'd/M/yy HH.mm') Unable to parse 2011-02-01 7.30 (format 'yyyy-MM-dd H.mm') Unable to parse 2011-02-01 07.30 (format 'yyyy-MM-dd HH.mm') Unable to parse 01/02/2011 7.30.45 (format 'dd/MM/yyyy H.mm.ss') Unable to parse 01/02/2011 07.30.45 (format 'dd/MM/yyyy HH.mm.ss') Unable to parse 01/Feb/2011 7.30.45 (format 'dd/MMM/yyyy H.mm.ss') Unable to parse 01/Feb/2011 07.30.45 (format 'dd/MMM/yyyy HH.mm.ss') Unable to parse 01/02/11 7.30.45 (format 'dd/MM/yy H.mm.ss') Unable to parse 01/02/11 07.30.45 (format 'dd/MM/yy HH.mm.ss') Unable to parse 01.2.11 7.30.45 (format 'dd.M.yy H.mm.ss') Unable to parse 01.2.11 07.30.45 (format 'dd.M.yy HH.mm.ss') Unable to parse 1/2/11 7.30.45 (format 'd/M/yy H.mm.ss') Unable to parse 1/2/11 07.30.45 (format 'd/M/yy HH.mm.ss') Unable to parse 2011-02-01 7.30.45 (format 'yyyy-MM-dd H.mm.ss') Unable to parse 2011-02-01 07.30.45 (format 'yyyy-MM-dd HH.mm.ss') Unable to parse Tuesday 1 February 2011 7.30.45 (format 'dddd d MMMM yyyy H.mm.ss') Unable to parse Tuesday 1 February 2011 07.30.45 (format 'dddd d MMMM yyyy HH.mm.ss') Unable to parse 1-Feb-11 7.30.45 (format 'd-MMM-yy H.mm.ss') Unable to parse 1-Feb-11 07.30.45 (format 'd-MMM-yy HH.mm.ss') Unable to parse 1 February 2011 7.30.45 (format 'd MMMM yyyy H.mm.ss') Unable to parse 1 February 2011 07.30.45 (format 'd MMMM yyyy HH.mm.ss') Unable to round-trip 42 of 98 format strings.
示例3: Main
//引入命名空间
using System;
using System.Globalization;
public class SamplesDateTimeFormatInfo {
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 void PrintIndexAndValues( String[] myArray ) {
int i = 0;
foreach ( String s in myArray )
Console.WriteLine( "\t[{0}]:\t{1}", i++, s );
Console.WriteLine();
}
}
输出:
ALL the patterns: [0]: MM/dd/yyyy [1]: dddd, dd MMMM yyyy [2]: dddd, dd MMMM yyyy HH:mm [3]: dddd, dd MMMM yyyy hh:mm tt [4]: dddd, dd MMMM yyyy H:mm [5]: dddd, dd MMMM yyyy h:mm tt [6]: dddd, dd MMMM yyyy HH:mm:ss [7]: MM/dd/yyyy HH:mm [8]: MM/dd/yyyy hh:mm tt [9]: MM/dd/yyyy H:mm [10]: MM/dd/yyyy h:mm tt [11]: MM/dd/yyyy HH:mm:ss [12]: MMMM dd [13]: MMMM dd [14]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT' [15]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT' [16]: yyyy'-'MM'-'dd'T'HH':'mm':'ss [17]: HH:mm [18]: hh:mm tt [19]: H:mm [20]: h:mm tt [21]: HH:mm:ss [22]: yyyy'-'MM'-'dd HH':'mm':'ss'Z' [23]: dddd, dd MMMM yyyy HH:mm:ss [24]: yyyy MMMM [25]: yyyy MMMM The patterns for 'd': [0]: MM/dd/yyyy The patterns for 'D': [0]: dddd, dd MMMM yyyy The patterns for 'f': [0]: dddd, dd MMMM yyyy HH:mm [1]: dddd, dd MMMM yyyy hh:mm tt [2]: dddd, dd MMMM yyyy H:mm [3]: dddd, dd MMMM yyyy h:mm tt The patterns for 'F': [0]: dddd, dd MMMM yyyy HH:mm:ss The patterns for 'r': [0]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT' The patterns for 'R': [0]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
示例4: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo culture = CultureInfo.CreateSpecificCulture("ru-RU");
char[] formats = { 'd', 'D', 'f', 'F', 'g', 'G', 'm', 'o',
'r', 's', 't', 'T', 'u', 'U', 'y' };
DateTime date1 = new DateTime(2011, 02, 01, 7, 30, 45, 0);
DateTime date2;
int total = 0;
int noRoundTrip = 0;
foreach (var fmt in formats) {
total = 0;
noRoundTrip = 0;
foreach (var pattern in culture.DateTimeFormat.GetAllDateTimePatterns(fmt)) {
total++;
if (! DateTime.TryParse(date1.ToString(pattern), out date2)) {
noRoundTrip++;
Console.WriteLine("Unable to parse {0:" + pattern + "} (format '{1}')",
date1, pattern);
}
}
if (noRoundTrip > 0)
Console.WriteLine("{0}: Unable to round-trip {1} of {2} format strings.\n",
fmt, noRoundTrip, total);
else
Console.WriteLine("{0}: All custom format strings round trip.\n", fmt);
}
}
}
输出:
d: All custom format strings round trip. Unable to parse 1 February 2011 г. (format 'd MMMM yyyy 'г.'') Unable to parse 01 February 2011 г. (format 'dd MMMM yyyy 'г.'') D: Unable to round-trip 2 of 2 format strings. Unable to parse 1 February 2011 г. 7:30 (format 'd MMMM yyyy 'г.' H:mm') Unable to parse 1 February 2011 г. 07:30 (format 'd MMMM yyyy 'г.' HH:mm') Unable to parse 01 February 2011 г. 7:30 (format 'dd MMMM yyyy 'г.' H:mm') Unable to parse 01 February 2011 г. 07:30 (format 'dd MMMM yyyy 'г.' HH:mm') f: Unable to round-trip 4 of 4 format strings. Unable to parse 1 February 2011 г. 7:30:45 (format 'd MMMM yyyy 'г.' H:mm:ss') Unable to parse 1 February 2011 г. 07:30:45 (format 'd MMMM yyyy 'г.' HH:mm:ss') Unable to parse 01 February 2011 г. 7:30:45 (format 'dd MMMM yyyy 'г.' H:mm:ss') Unable to parse 01 February 2011 г. 07:30:45 (format 'dd MMMM yyyy 'г.' HH:mm:ss') F: Unable to round-trip 4 of 4 format strings. g: All custom format strings round trip. G: All custom format strings round trip. m: All custom format strings round trip. o: All custom format strings round trip. r: All custom format strings round trip. s: All custom format strings round trip. t: All custom format strings round trip. T: All custom format strings round trip. u: All custom format strings round trip. Unable to parse 1 February 2011 г. 7:30:45 (format 'd MMMM yyyy 'г.' H:mm:ss') Unable to parse 1 February 2011 г. 07:30:45 (format 'd MMMM yyyy 'г.' HH:mm:ss') Unable to parse 01 February 2011 г. 7:30:45 (format 'dd MMMM yyyy 'г.' H:mm:ss') Unable to parse 01 February 2011 г. 07:30:45 (format 'dd MMMM yyyy 'г.' HH:mm:ss') U: Unable to round-trip 4 of 4 format strings. y: All custom format strings round trip.
示例5: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class MainClass
{
public static void Main()
{
DateTime dt1 = new DateTime(2004, 10, 9, 22, 47, 35, 259);
DateTimeFormatInfo di = new DateTimeFormatInfo();
for (char c = 'a'; c <= 'z'; c++)
{
try
{
foreach (string s in di.GetAllDateTimePatterns(c))
{
Console.WriteLine("'{0}': {1} - {2}/{3}", c, s,
dt1.ToString(c.ToString()), dt1.ToString(s));
}
char cUpper = Char.ToUpper(c);
foreach (string s in di.GetAllDateTimePatterns(cUpper))
{
Console.WriteLine("'{0}': {1} - {2}", cUpper, s,
dt1.ToString(cUpper.ToString()), dt1.ToString(s));
}
}
catch (ArgumentException)
{
// Ignore--specifier not found.
}
}
}
}