一聚教程网:一个值得你收藏的教程网站

热门教程

c# datetime格式化输出与实例用法

时间:2022-06-25 08:27:03 编辑:袖梨 来源:一聚教程网

c# datetime格式化输出与实例用法

// 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

实例

Program that uses DateTime format [C#]

using System;

class Program
{
    static void Main()
    {
 DateTime time = DateTime.Now;              // Use current time
 string format = "MMM ddd d HH:mm yyyy";    // Use this format
 Console.WriteLine(time.ToString(format));  // Write to console
    }
}

Output

Feb Fri 27 11:41 2009

Format string pattern

MMM     display three-letter month
ddd     display three-letter day of the WEEK
d       display day of the MONTH
HH      display two-digit hours on 24-hour scale
mm      display two-digit minutes
yyyy    display four-digit year

另种做法

using System;

class Program
{
    static void Main()
    {
 DateTime time = DateTime.Now;             // Use current time
 string format = "M d h:mm yy";            // Use this format
 Console.WriteLine(time.ToString(format)); // Write to console
    }
}

Output

2 27 11:48 09

Format string pattern

M       display one-digit month number          [changed]
d       display one-digit day of the MONTH      [changed]
h       display one-digit hour on 12-hour scale [changed]
mm      display two-digit minutes
yy      display two-digit year                  [changed]

国外在常用格式

using System;

class Program
{
    static void Main()
    {
 DateTime now = DateTime.Now;
 Console.WriteLine(now.ToString("d"));
 Console.WriteLine(now.ToString("D"));
 Console.WriteLine(now.ToString("f"));
 Console.WriteLine(now.ToString("F"));
 Console.WriteLine(now.ToString("g"));
 Console.WriteLine(now.ToString("G"));
 Console.WriteLine(now.ToString("m"));
 Console.WriteLine(now.ToString("M"));
 Console.WriteLine(now.ToString("o"));
 Console.WriteLine(now.ToString("O"));
 Console.WriteLine(now.ToString("s"));
 Console.WriteLine(now.ToString("t"));
 Console.WriteLine(now.ToString("T"));
 Console.WriteLine(now.ToString("u"));
 Console.WriteLine(now.ToString("U"));
 Console.WriteLine(now.ToString("y"));
 Console.WriteLine(now.ToString("Y"));
    }
}

Output

d    2/27/2009
D    Friday, February 27, 2009
f    Friday, February 27, 2009 12:11 PM
F    Friday, February 27, 2009 12:12:22 PM
g    2/27/2009 12:12 PM
G    2/27/2009 12:12:22 PM
m    February 27
M    February 27
o    2009-02-27T12:12:22.1020000-08:00
O    2009-02-27T12:12:22.1020000-08:00
s    2009-02-27T12:12:22
t    12:12 PM
T    12:12:22 PM
u    2009-02-27 12:12:22Z
U    Friday, February 27, 2009 8:12:22 PM
y    February, 2009
Y    February, 2009

using System;

class Program
{
    static void Main()
    {
 DateTime now = DateTime.Now;
 Console.WriteLine(now.ToLongDateString());  // Equivalent to D
 Console.WriteLine(now.ToLongTimeString());  // Equivalent to T
 Console.WriteLine(now.ToShortDateString()); // Equivalent to d
 Console.WriteLine(now.ToShortTimeString()); // Equivalent to t
 Console.WriteLine(now.ToString());
    }
}

Output

ToLongDateString     Friday, February 27, 2009
ToLongTimeString     12:16:59 PM
ToShortDateString    2/27/2009
ToShortTimeString    12:16 PM
ToString             2/27/2009 12:16:59 PM

热门栏目