満足するまで編集、コンパイル、gawk、および再帰という骨の折れるプロセスに着手するのではなく、さまざまな DateTime 形式がどのように表示されるかをすばやく参照できるようにしたいと考えています。
4 に答える
これは、現在のカルチャのすべての形式を示しています。
StringBuilder sb = new StringBuilder();
foreach (string dateTimePattern in DateTimeFormatInfo.CurrentInfo.GetAllDateTimePatterns()) {
sb.AppendFormat("{0} = {1}\r\n", dateTimePattern, DateTime.Now.ToString(dateTimePattern));
}
後の StringBuilder の内容:
M/d/yyyy = 7/18/2012
M/d/yy = 7/18/12
MM/dd/yy = 07/18/12
MM/dd/yyyy = 07/18/2012
yy/MM/dd = 12/07/18
yyyy-MM-dd = 2012-07-18
dd-MMM-yy = 18-Jul-12
dddd, MMMM dd, yyyy = Wednesday, July 18, 2012
MMMM dd, yyyy = July 18, 2012
dddd, dd MMMM, yyyy = Wednesday, 18 July, 2012
dd MMMM, yyyy = 18 July, 2012
dddd, MMMM dd, yyyy h:mm tt = Wednesday, July 18, 2012 4:25 PM
dddd, MMMM dd, yyyy hh:mm tt = Wednesday, July 18, 2012 04:25 PM
dddd, MMMM dd, yyyy H:mm = Wednesday, July 18, 2012 16:25
dddd, MMMM dd, yyyy HH:mm = Wednesday, July 18, 2012 16:25
MMMM dd, yyyy h:mm tt = July 18, 2012 4:25 PM
MMMM dd, yyyy hh:mm tt = July 18, 2012 04:25 PM
MMMM dd, yyyy H:mm = July 18, 2012 16:25
MMMM dd, yyyy HH:mm = July 18, 2012 16:25
dddd, dd MMMM, yyyy h:mm tt = Wednesday, 18 July, 2012 4:25 PM
dddd, dd MMMM, yyyy hh:mm tt = Wednesday, 18 July, 2012 04:25 PM
dddd, dd MMMM, yyyy H:mm = Wednesday, 18 July, 2012 16:25
dddd, dd MMMM, yyyy HH:mm = Wednesday, 18 July, 2012 16:25
dd MMMM, yyyy h:mm tt = 18 July, 2012 4:25 PM
dd MMMM, yyyy hh:mm tt = 18 July, 2012 04:25 PM
dd MMMM, yyyy H:mm = 18 July, 2012 16:25
dd MMMM, yyyy HH:mm = 18 July, 2012 16:25
dddd, MMMM dd, yyyy h:mm:ss tt = Wednesday, July 18, 2012 4:25:17 PM
dddd, MMMM dd, yyyy hh:mm:ss tt = Wednesday, July 18, 2012 04:25:17 PM
dddd, MMMM dd, yyyy H:mm:ss = Wednesday, July 18, 2012 16:25:17
dddd, MMMM dd, yyyy HH:mm:ss = Wednesday, July 18, 2012 16:25:17
MMMM dd, yyyy h:mm:ss tt = July 18, 2012 4:25:17 PM
MMMM dd, yyyy hh:mm:ss tt = July 18, 2012 04:25:17 PM
MMMM dd, yyyy H:mm:ss = July 18, 2012 16:25:17
MMMM dd, yyyy HH:mm:ss = July 18, 2012 16:25:17
dddd, dd MMMM, yyyy h:mm:ss tt = Wednesday, 18 July, 2012 4:25:17 PM
dddd, dd MMMM, yyyy hh:mm:ss tt = Wednesday, 18 July, 2012 04:25:17 PM
dddd, dd MMMM, yyyy H:mm:ss = Wednesday, 18 July, 2012 16:25:17
dddd, dd MMMM, yyyy HH:mm:ss = Wednesday, 18 July, 2012 16:25:17
dd MMMM, yyyy h:mm:ss tt = 18 July, 2012 4:25:17 PM
dd MMMM, yyyy hh:mm:ss tt = 18 July, 2012 04:25:17 PM
dd MMMM, yyyy H:mm:ss = 18 July, 2012 16:25:17
dd MMMM, yyyy HH:mm:ss = 18 July, 2012 16:25:17
M/d/yyyy h:mm tt = 7/18/2012 4:25 PM
M/d/yyyy hh:mm tt = 7/18/2012 04:25 PM
M/d/yyyy H:mm = 7/18/2012 16:25
M/d/yyyy HH:mm = 7/18/2012 16:25
M/d/yy h:mm tt = 7/18/12 4:25 PM
M/d/yy hh:mm tt = 7/18/12 04:25 PM
M/d/yy H:mm = 7/18/12 16:25
M/d/yy HH:mm = 7/18/12 16:25
MM/dd/yy h:mm tt = 07/18/12 4:25 PM
MM/dd/yy hh:mm tt = 07/18/12 04:25 PM
MM/dd/yy H:mm = 07/18/12 16:25
MM/dd/yy HH:mm = 07/18/12 16:25
MM/dd/yyyy h:mm tt = 07/18/2012 4:25 PM
MM/dd/yyyy hh:mm tt = 07/18/2012 04:25 PM
MM/dd/yyyy H:mm = 07/18/2012 16:25
MM/dd/yyyy HH:mm = 07/18/2012 16:25
yy/MM/dd h:mm tt = 12/07/18 4:25 PM
yy/MM/dd hh:mm tt = 12/07/18 04:25 PM
yy/MM/dd H:mm = 12/07/18 16:25
yy/MM/dd HH:mm = 12/07/18 16:25
yyyy-MM-dd h:mm tt = 2012-07-18 4:25 PM
yyyy-MM-dd hh:mm tt = 2012-07-18 04:25 PM
yyyy-MM-dd H:mm = 2012-07-18 16:25
yyyy-MM-dd HH:mm = 2012-07-18 16:25
dd-MMM-yy h:mm tt = 18-Jul-12 4:25 PM
dd-MMM-yy hh:mm tt = 18-Jul-12 04:25 PM
dd-MMM-yy H:mm = 18-Jul-12 16:25
dd-MMM-yy HH:mm = 18-Jul-12 16:25
M/d/yyyy h:mm:ss tt = 7/18/2012 4:25:17 PM
M/d/yyyy hh:mm:ss tt = 7/18/2012 04:25:17 PM
M/d/yyyy H:mm:ss = 7/18/2012 16:25:17
M/d/yyyy HH:mm:ss = 7/18/2012 16:25:17
M/d/yy h:mm:ss tt = 7/18/12 4:25:17 PM
M/d/yy hh:mm:ss tt = 7/18/12 04:25:17 PM
M/d/yy H:mm:ss = 7/18/12 16:25:17
M/d/yy HH:mm:ss = 7/18/12 16:25:17
MM/dd/yy h:mm:ss tt = 07/18/12 4:25:17 PM
MM/dd/yy hh:mm:ss tt = 07/18/12 04:25:17 PM
MM/dd/yy H:mm:ss = 07/18/12 16:25:17
MM/dd/yy HH:mm:ss = 07/18/12 16:25:17
MM/dd/yyyy h:mm:ss tt = 07/18/2012 4:25:17 PM
MM/dd/yyyy hh:mm:ss tt = 07/18/2012 04:25:17 PM
MM/dd/yyyy H:mm:ss = 07/18/2012 16:25:17
MM/dd/yyyy HH:mm:ss = 07/18/2012 16:25:17
yy/MM/dd h:mm:ss tt = 12/07/18 4:25:17 PM
yy/MM/dd hh:mm:ss tt = 12/07/18 04:25:17 PM
yy/MM/dd H:mm:ss = 12/07/18 16:25:17
yy/MM/dd HH:mm:ss = 12/07/18 16:25:17
yyyy-MM-dd h:mm:ss tt = 2012-07-18 4:25:17 PM
yyyy-MM-dd hh:mm:ss tt = 2012-07-18 04:25:17 PM
yyyy-MM-dd H:mm:ss = 2012-07-18 16:25:17
yyyy-MM-dd HH:mm:ss = 2012-07-18 16:25:17
dd-MMM-yy h:mm:ss tt = 18-Jul-12 4:25:17 PM
dd-MMM-yy hh:mm:ss tt = 18-Jul-12 04:25:17 PM
dd-MMM-yy H:mm:ss = 18-Jul-12 16:25:17
dd-MMM-yy HH:mm:ss = 18-Jul-12 16:25:17
MMMM dd = July 18
MMMM dd = July 18
yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK = 2012-07-18T16:25:17.0961142-07:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK = 2012-07-18T16:25:17.0961142-07:00
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' = Wed, 18 Jul 2012 16:25:17 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' = Wed, 18 Jul 2012 16:25:17 GMT
yyyy'-'MM'-'dd'T'HH':'mm':'ss = 2012-07-18T16:25:17
h:mm tt = 4:25 PM
hh:mm tt = 04:25 PM
H:mm = 16:25
HH:mm = 16:25
h:mm:ss tt = 4:25:17 PM
hh:mm:ss tt = 04:25:17 PM
H:mm:ss = 16:25:17
HH:mm:ss = 16:25:17
yyyy'-'MM'-'dd HH':'mm':'ss'Z' = 2012-07-18 16:25:17Z
dddd, MMMM dd, yyyy h:mm:ss tt = Wednesday, July 18, 2012 4:25:17 PM
dddd, MMMM dd, yyyy hh:mm:ss tt = Wednesday, July 18, 2012 04:25:17 PM
dddd, MMMM dd, yyyy H:mm:ss = Wednesday, July 18, 2012 16:25:17
dddd, MMMM dd, yyyy HH:mm:ss = Wednesday, July 18, 2012 16:25:17
MMMM dd, yyyy h:mm:ss tt = July 18, 2012 4:25:17 PM
MMMM dd, yyyy hh:mm:ss tt = July 18, 2012 04:25:17 PM
MMMM dd, yyyy H:mm:ss = July 18, 2012 16:25:17
MMMM dd, yyyy HH:mm:ss = July 18, 2012 16:25:17
dddd, dd MMMM, yyyy h:mm:ss tt = Wednesday, 18 July, 2012 4:25:17 PM
dddd, dd MMMM, yyyy hh:mm:ss tt = Wednesday, 18 July, 2012 04:25:17 PM
dddd, dd MMMM, yyyy H:mm:ss = Wednesday, 18 July, 2012 16:25:17
dddd, dd MMMM, yyyy HH:mm:ss = Wednesday, 18 July, 2012 16:25:17
dd MMMM, yyyy h:mm:ss tt = 18 July, 2012 4:25:17 PM
dd MMMM, yyyy hh:mm:ss tt = 18 July, 2012 04:25:17 PM
dd MMMM, yyyy H:mm:ss = 18 July, 2012 16:25:17
dd MMMM, yyyy HH:mm:ss = 18 July, 2012 16:25:17
MMMM, yyyy = July, 2012
MMMM, yyyy = July, 2012
いくつかのパターンが繰り返される理由はわかりません。
カスタム日時フォーマット
次のカスタム書式指定子があります y (年)、M (月)、d (日)、h (12 時)、H (24 時)、m (分)、s (秒)、f (秒の小数部)、F (2 番目の端数、末尾のゼロは削除されます)、t (PM または AM)、および z (タイム ゾーン)。
次の例は、フォーマット指定子が出力にどのように書き換えられるかを示しています。[C#]
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
日付区切り記号 / (スラッシュ) と時刻区切り記号 : (コロン) も使用できます。これらの文字は、現在の DateTimeFormatInfo.DateSeparator および DateTimeFormatInfo.TimeSeparator で定義されている文字に書き換えられます。
// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE)
カスタムの日付と時刻の書式設定の例を次に示します。
// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt); // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"
// day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt); // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt); // "Sunday, March 9, 2008"
// two/four digit year
String.Format("{0:MM/dd/yy}", dt); // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"
標準の日時フォーマット
DateTimeFormatInfo には、現在のカルチャの標準パターンが定義されています。たとえば、プロパティ ShortTimePattern は、en-US カルチャの値 h:mm tt と de-DE カルチャの値 HH:mm を含む文字列です。
次の例は、String.Format メソッドでの標準書式指定子の使用と結果の出力を示しています。
String.Format("{0:t}", dt); //"4:05 PM" ShortTime
String.Format("{0:d}", dt); //"3/9/2008" ShortDate
String.Format("{0:T}", dt); //"4:05:07 PM" LongTime
String.Format("{0:D}", dt); //"Sunday, March 09, 2008" LongDate
String.Format("{0:f}", dt); //"Sunday, March 09, 2008 4:05 PM" LongDate+ShortTime
String.Format("{0:F}", dt); //"Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt); //"3/9/2008 4:05 PM" ShortDate+ShortTime
String.Format("{0:G}", dt); //"3/9/2008 4:05:07 PM" ShortDate+LongTime
String.Format("{0:m}", dt); //"March 09" MonthDay
String.Format("{0:y}", dt); //"March, 2008" YearMonth
String.Format("{0:r}", dt); //"Sun, 09 Mar 2008 16:05:07 GMT" RFC1123
String.Format("{0:s}", dt); //"2008-03-09T16:05:07" SortableDateTime
String.Format("{0:u}", dt); // "2008-03-09 16:05:07Z" UniversalSortableDateTime
私はこれを一度行いました...私がしたことは、Windowsアプリケーションを作成し、ラベルとテキストボックスを配置し、テキストボックス変更イベントにアタッチすることでした
Label1.Text = DateTime.Now.ToString(TextBox1.Text)
非常にシンプルで、すぐに試してみるのに役立ちます。
DateTime.Now.ToString("D") displays "Wednesday, July 18, 2012"
DateTime.Now.ToString("d") displays "7/18/2012"
DateTime.Now.ToString("F") displays "Wednesday, July 18, 2012 3:57:13 PM"
DateTime.Now.ToString("f") displays "Wednesday, July 18, 2012 3:57 PM"
DateTime.Now.ToString("G") displays "7/18/2012 3:57 PM"
DateTime.Now.ToString("g") displays "7/18/2012 3:57:30 PM"
DateTime.Now.ToString("M") displays "July 18"
DateTime.Now.ToString("R") displays "Wed, 18 Jul 2012 15:57:55 GMT"
DateTime.Now.ToString("s") displays "2012-07-18T15:58:02"
DateTime.Now.ToString("T") displays "3:56:38 PM"
DateTime.Now.ToString("t") displays "3:56 PM"
DateTime.Now.ToString("U") displays "Wednesday, July 18, 2012 10:58:09 PM"
DateTime.Now.ToString("u") displays "2012-07-18 15:58:17Z"
DateTime.Now.ToString("Y") displays "July, 2012"
DateTime.Now.ToString("42") displays "42"