DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("yyyy-MM-dd")); //按yyyy-MM-dd格式输出s
Console.WriteLine(dt.ToString()); // 26/11/2009 AM 11:21:30
Console.WriteLine(dt.ToFileTime().ToString()); // 129036792908014024
// Converts the value of the current System.DateTime object to a Windows file time
Console.WriteLine(dt.ToFileTimeUtc().ToString()); // 129036792908014024
// Converts the value of the current System.DateTime object to a Windows file time
Console.WriteLine(dt.ToLocalTime().ToString()); // 26/11/2009 AM 11:21:30
// Converts the value of the current System.DateTime object to local time.
Console.WriteLine(dt.ToLongDateString().ToString()); // 2009年11月26日
Console.WriteLine(dt.ToLongTimeString().ToString()); // AM 11:21:30
Console.WriteLine(dt.ToOADate().ToString()); // 40143.4732731597
Console.WriteLine(dt.ToShortDateString().ToString()); // 26/11/2009
Console.WriteLine(dt.ToShortTimeString().ToString()); // AM 11:21
Console.WriteLine(dt.ToUniversalTime().ToString()); // 26/11/2009 AM 3:21:30
Console.WriteLine(dt.Year.ToString()); // 2009
Console.WriteLine(dt.Date.ToString()); // 26/11/2009 AM 12:00:00
Console.WriteLine(dt.DayOfWeek.ToString()); // Thursday
Console.WriteLine(dt.DayOfYear.ToString()); // 330
Console.WriteLine(dt.Hour.ToString()); // 11
Console.WriteLine(dt.Millisecond.ToString()); // 801 (毫秒)
Console.WriteLine(dt.Minute.ToString()); // 21
Console.WriteLine(dt.Month.ToString()); // 11
Console.WriteLine(dt.Second.ToString()); // 30
Console.WriteLine(dt.Ticks.ToString()); // 633948312908014024
Console.WriteLine(dt.TimeOfDay.ToString()); // 12:29:51.5181524
// Gets the time of day for this instance.
// 返回 A System.TimeSpan that represents the fraction of the day that has elapsed since midnight.
Console.WriteLine(dt.ToString()); // 26/11/2009 PM 12:29:51
Console.WriteLine(dt.AddYears(1).ToString()); // 26/11/2010 PM 12:29:51
Console.WriteLine(dt.AddDays(1.1).ToString()); // 27/11/2009 PM 2:53:51
Console.WriteLine(dt.AddHours(1.1).ToString()); // 26/11/2009 PM 1:35:51
Console.WriteLine(dt.AddMilliseconds(1.1).ToString()); //26/11/2009 PM 12:29:51
Console.WriteLine(dt.AddMonths(1).ToString()); // 26/12/2009 PM 12:29:51
Console.WriteLine(dt.AddSeconds(1.1).ToString()); // 26/11/2009 PM 12:29:52
Console.WriteLine(dt.AddMinutes(1.1).ToString()); // 26/11/2009 PM 12:30:57
Console.WriteLine(dt.AddTicks(1000).ToString()); // 26/11/2009 PM 12:29:51
Console.WriteLine(dt.CompareTo(dt).ToString()); // 0
Console.WriteLine(dt.Add(new TimeSpan(1,0,0,0)).ToString()); // 加上一个时间段
(注:
System.TimeSpan为一个时间段,构造函数如下
public TimeSpan(long ticks); // ticks: A time period expressed in 100-nanosecond units.
//nanosecond:十亿分之一秒 new TimeSpan(10,000,000) 为一秒
public TimeSpan(int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);
)
Console.WriteLine(dt.Equals("2005-11-6 16:11:04").ToString()); // False
Console.WriteLine(dt.Equals(dt).ToString()); // True
Console.WriteLine(dt.GetHashCode().ToString()); // 1103291775
Console.WriteLine(dt.GetType().ToString()); // System.DateTime
Console.WriteLine(dt.GetTypeCode().ToString()); // DateTime
long Start = Environment.TickCount; //单位是毫秒
long End = Environment.TickCount;
Console.WriteLine("Start is : "+Start);
Console.WriteLine("End is : "+End);
Console.WriteLine("The Time is {0}",End-Start);
Console.WriteLine(dt.GetDateTimeFormats('s')[0].ToString()); //2009-11-26T13:29:06
Console.WriteLine(dt.GetDateTimeFormats('t')[0].ToString()); //PM 1:29
Console.WriteLine(dt.GetDateTimeFormats('y')[0].ToString()); //2009年11月
Console.WriteLine(dt.GetDateTimeFormats('D')[0].ToString()); //2009年11月26日
Console.WriteLine(dt.GetDateTimeFormats('D')[1].ToString()); //星期四, 26 十一月, 2009
Console.WriteLine(dt.GetDateTimeFormats('D')[2].ToString()); //26 十一月, 2009
Console.WriteLine(dt.GetDateTimeFormats('D')[3].ToString()); //星期四 2009 11 26
Console.WriteLine(dt.GetDateTimeFormats('M')[0].ToString()); //26 十一月
Console.WriteLine(dt.GetDateTimeFormats('f')[0].ToString()); //2009年11月26日 PM 1:29
Console.WriteLine(dt.GetDateTimeFormats('g')[0].ToString()); //26/11/2009 PM 1:29
Console.WriteLine(dt.GetDateTimeFormats('r')[0].ToString()); //Thu, 26 Nov 2009 13:29:06 GMT
(注:
常用的日期时间格式:
格式 说明 输出格式
d 精简日期格式 MM/dd/yyyy
D 详细日期格式 dddd, MMMM dd, yyyy
f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
F 完整日期时间格式 (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss
g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
m,M 月日格式 MMMM dd
s 适中日期时间格式 yyyy-MM-dd HH:mm:ss
t 精简时间格式 HH:mm
T 详细时间格式 HH:mm:ss
)
Console.WriteLine(string.Format("{0:d}", dt)); //28/12/2009
Console.WriteLine(string.Format("{0:D}", dt)); //2009年12月28日
Console.WriteLine(string.Format("{0:f}", dt)); //2009年12月28日 AM 10:29
Console.WriteLine(string.Format("{0:F}", dt)); //2009年12月28日 AM 10:29:18
Console.WriteLine(string.Format("{0:g}", dt)); //28/12/2009 AM 10:29
Console.WriteLine(string.Format("{0:G}", dt)); //28/12/2009 AM 10:29:18
Console.WriteLine(string.Format("{0:M}", dt)); //28 十二月
Console.WriteLine(string.Format("{0:R}", dt)); //Mon, 28 Dec 2009 10:29:18 GMT
Console.WriteLine(string.Format("{0:s}", dt)); //2009-12-28T10:29:18
Console.WriteLine(string.Format("{0:t}", dt)); //AM 10:29
Console.WriteLine(string.Format("{0:T}", dt)); //AM 10:29:18
Console.WriteLine(string.Format("{0:u}", dt)); //2009-12-28 10:29:18Z
Console.WriteLine(string.Format("{0:U}", dt)); //2009年12月28日 AM 2:29:18
Console.WriteLine(string.Format("{0:Y}", dt)); //2009年12月
Console.WriteLine(string.Format("{0}", dt)); //28/12/2009 AM 10:29:18
Console.WriteLine(string.Format("{0:yyyyMMddHHmmssffff}", dt)); //200912281029182047
1DateTime dt = DateTime.Now;
2// Label1.Text = dt.ToString();//2005-11-5 13:21:25
3// Label2.Text = dt.ToFileTime().ToString();//127756416859912816
4// Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
5// Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
6// Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
7// Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
8// Label7.Text = dt.ToOADate().ToString();//38661.5565508218
9// Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
10// Label9.Text = dt.ToShortTimeString().ToString();//13:21
11// Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
12
13// 2005-11-5 13:30:28.4412864
14// Label1.Text = dt.Year.ToString();//2005
15// Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
16// Label3.Text = dt.DayOfWeek.ToString();//Saturday
17// Label4.Text = dt.DayOfYear.ToString();//309
18// Label5.Text = dt.Hour.ToString();//13
19// Label6.Text = dt.Millisecond.ToString();//441
20// Label7.Text = dt.Minute.ToString();//30
21// Label8.Text = dt.Month.ToString();//11
22// Label9.Text = dt.Second.ToString();//28
23// Label10.Text = dt.Ticks.ToString();//632667942284412864
24// Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
25// Label1.Text = dt.ToString();//2005-11-5 13:47:04
26// Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
27// Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
28// Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
29// Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
30// Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
31// Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
32// Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
33// Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
34// Label10.Text = dt.CompareTo(dt).ToString();//0
35/**///// Label11.Text = dt.Add(?).ToString();//问号为一个时间段
36// Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
37// Label2.Text = dt.Equals(dt).ToString();//True
38// Label3.Text = dt.GetHashCode().ToString();//1474088234
39// Label4.Text = dt.GetType().ToString();//System.DateTime
40// Label5.Text = dt.GetTypeCode().ToString();//DateTime
41
42// Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
43// Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
44// Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
45// Label4.Text = dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
46// Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
47// Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
48// Label7.Text = dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
49// Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11月5日
50// Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06
51// Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
52// Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
53
54 Label1.Text = string.Format("{0:d}",dt);//2005-11-5
55 Label2.Text = string.Format("{0:D}",dt);//2005年11月5日
56 Label3.Text = string.Format("{0:f}",dt);//2005年11月5日 14:23
57 Label4.Text = string.Format("{0:F}",dt);//2005年11月5日 14:23:23
58 Label5.Text = string.Format("{0:g}",dt);//2005-11-5 14:23
59 Label6.Text = string.Format("{0:G}",dt);//2005-11-5 14:23:23
60 Label7.Text = string.Format("{0:M}",dt);//11月5日
61 Label8.Text = string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
62 Label9.Text = string.Format("{0:s}",dt);//2005-11-05T14:23:23
63 Label10.Text = string.Format("{0:t}",dt);//14:23
64 Label11.Text = string.Format("{0:T}",dt);//14:23:23
65 Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
66 Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
67 Label14.Text = string.Format("{0:Y}",dt);//2005年11月
68 Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23
69 Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);