format in datetime

来源:互联网 发布:英国女王 知乎 编辑:程序博客网 时间:2024/04/30 14:48

using System;
using System.Globalization;

public class MainClass {
   public static void Main(string[] args)  {
       DateTime dt = DateTime.Now;
       String[] format = {
           "d", "D",
           "f", "F",
           "g", "G",
           "m",
           "r",
           "s",
           "t", "T",
           "u", "U",
           "y",
           "dddd, MMMM dd yyyy",
           "ddd, MMM d /"'/"yy",
           "dddd, MMMM dd",
           "M/yy",
           "dd-MM-yy",
       };
       String date;
       for (int i = 0; i < format.Length; i++) {
           date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
           Console.WriteLine(String.Concat(format[i], " :" , date));
       }
 
  /** Output.
   *
   * d :08/17/2000
   * D :Thursday, August 17, 2000
   * f :Thursday, August 17, 2000 16:32
   * F :Thursday, August 17, 2000 16:32:32
   * g :08/17/2000 16:32
   * G :08/17/2000 16:32:32
   * m :August 17
   * r :Thu, 17 Aug 2000 23:32:32 GMT
   * s :2000-08-17T16:32:32
   * t :16:32
   * T :16:32:32
   * u :2000-08-17 23:32:32Z
   * U :Thursday, August 17, 2000 23:32:32
   * y :August, 2000
   * dddd, MMMM dd yyyy :Thursday, August 17 2000
   * ddd, MMM d "'"yy :Thu, Aug 17 '00
   * dddd, MMMM dd :Thursday, August 17
   * M/yy :8/00
   * dd-MM-yy :17-08-00
   */
   }
}

Format CharacterAssociated Property/ Description
dShortDatePattern
DLongDatePattern
fFull date and time (long date and short time)
FFullDateTimePattern (long date and long time)
gGeneral (short date and short time)
GGeneral (short date and long time)
m, MMonthDayPattern
r, RRFC1123Pattern
sSortableDateTimePattern (based on ISO 8601) using local time
tShortTimePattern
TLongTimePattern
uUniversalSortableDateTimePattern using the format for universal time display
UFull date and time (long date and long time) using universal time
y, YYearMonthPattern

The following table lists the patterns that can be combined to construct custom patterns. The patterns are case-sensitive; for example, "MM" is recognized, but "mm" is not. If the custom pattern contains white-space characters or characters enclosed in single quotation marks, the output string will also contain those characters. Characters not defined as part of a format pattern or as format characters are reproduced literally.

Format PatternDescription
dThe day of the month. Single-digit days will not have a leading zero.
ddThe day of the month. Single-digit days will have a leading zero.
dddThe abbreviated name of the day of the week, as defined in AbbreviatedDayNames.
ddddThe full name of the day of the week, as defined in DayNames.
MThe numeric month. Single-digit months will not have a leading zero.
MMThe numeric month. Single-digit months will have a leading zero.
MMMThe abbreviated name of the month, as defined in AbbreviatedMonthNames.
MMMMThe full name of the month, as defined in MonthNames.
yThe year without the century. If the year without the century is less than 10, the year is displayed with no leading zero.
yyThe year without the century. If the year without the century is less than 10, the year is displayed with a leading zero.
yyyyThe year in four digits, including the century.
ggThe period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string.
hThe hour in a 12-hour clock. Single-digit hours will not have a leading zero.
hhThe hour in a 12-hour clock. Single-digit hours will have a leading zero.
HThe hour in a 24-hour clock. Single-digit hours will not have a leading zero.
HHThe hour in a 24-hour clock. Single-digit hours will have a leading zero.
mThe minute. Single-digit minutes will not have a leading zero.
mmThe minute. Single-digit minutes will have a leading zero.
sThe second. Single-digit seconds will not have a leading zero.
ssThe second. Single-digit seconds will have a leading zero.
fThe fraction of a second in single-digit precision. The remaining digits are truncated.
ffThe fraction of a second in double-digit precision. The remaining digits are truncated.
fffThe fraction of a second in three-digit precision. The remaining digits are truncated.
ffffThe fraction of a second in four-digit precision. The remaining digits are truncated.
fffffThe fraction of a second in five-digit precision. The remaining digits are truncated.
ffffffThe fraction of a second in six-digit precision. The remaining digits are truncated.
fffffffThe fraction of a second in seven-digit precision. The remaining digits are truncated.
tThe first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any.
ttThe AM/PM designator defined in AMDesignator or PMDesignator, if any.
zThe time zone offset ("+" or "-" followed by the hour only). Single-digit hours will not have a leading zero. For example, Pacific Standard Time is "-8".
zzThe time zone offset ("+" or "-" followed by the hour only). Single-digit hours will have a leading zero. For example, Pacific Standard Time is "-08".
zzzThe full time zone offset ("+" or "-" followed by the hour and minutes). Single-digit hours and minutes will have leading zeros. For example, Pacific Standard Time is "-08:00".
:The default time separator defined in TimeSeparator.
/The default date separator defined in DateSeparator.
% c Where c is a format pattern if used alone. The "%" character can be omitted if the format pattern is combined with literal characters or other format patterns.
/ c Where c is any character. Displays the character literally. To display the backslash character, use "//".
原创粉丝点击