
·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> C#DateTime的ToString()方法的使用
1 Console.WriteLine("ToShortDateString:" + DateTime.Now.ToShortDateString());
2 Console.WriteLine("ToLongDateString:" + DateTime.Now.ToLongDateString());
3
4 Console.WriteLine("ToShortTimeString:" + DateTime.Now.ToShortTimeString());
5 Console.WriteLine("ToLongTimeString:" + DateTime.Now.ToLongTimeString());
6
7 Console.WriteLine(DateTime.Now.ToString());
8 Console.WriteLine(DateTime.Now.ToString("t"));
9
10 Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
11 Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"));
12
13 Console.WriteLine(DateTime.Now.ToString("d")); //格式: 2010-10-22
14 Console.WriteLine(DateTime.Now.ToString("D")); //格式: 2010年10月22日
15
16 Console.WriteLine(DateTime.Now.ToString("f")); //格式: 2010年10月22日 9:26
17 Console.WriteLine(DateTime.Now.ToString("F")); //格式: 2010年10月22日 9:26:38
18
19 Console.WriteLine(DateTime.Now.ToString("g")); //格式: 2010-10-22 9:26
20 Console.WriteLine(DateTime.Now.ToString("G")); //格式: 2010-10-22 9:26:38
21
22 Console.WriteLine(DateTime.Now.ToString("m")); //格式: 10月22日
23 Console.WriteLine(DateTime.Now.ToString("r")); //格式: Fri, 22 Oct 2010 09:26:38 GMT
24 Console.WriteLine(DateTime.Now.ToString("s")); //格式: 2010-10-22T09:26:38
25
26 Console.WriteLine(DateTime.Now.ToString("t")); //格式: 9:26
27 Console.WriteLine(DateTime.Now.ToString("T")); //格式: 9:26:38
28
29 Console.WriteLine(DateTime.Now.ToString("u")); //格式: 2010-10-22 09:26:38Z
30 Console.WriteLine(DateTime.Now.ToString("U")); //格式: 2010年10月22日 1:26:38
31
32 Console.WriteLine(DateTime.Now.ToString("y")); //格式: 2010年10月
33 Console.WriteLine(DateTime.Now.ToString("dddd")); //格式: 星期五
34 Console.WriteLine(DateTime.Now.ToString("dddd, MMMM dd yyyy")); //格式: 星期五, 十月 22 2010
35 Console.WriteLine(DateTime.Now.ToString("ddd, MMM d yy")); //格式: 五, 十月 22 10
36 Console.WriteLine(DateTime.Now.ToString("dddd, MMMM dd")); //格式: 星期五, 十月 22
37 Console.WriteLine(DateTime.Now.ToString("M/yy")); //格式: 10-10
38 Console.WriteLine(DateTime.Now.ToString("dd-MM-yy")); //格式: 22-10-10
特别注意:
由于服务器的日期时间格式设置与C#中的方法:DateTime.Now.ToShortDateString()产生的日期时间格式紧密相连。
后续正式环境请注意服务器的《日期 时间 格式设置》
短日期:yyyy-MM-dd
长日期:yyyy'年'M'月'd'日'
短时间:HH:mm
长时间:HH:mm:ss
同时还可直接使用如下方式直接获取自己想要得到的日期时间格式(似乎这种方式不服务器《日期 时间 格式设置》的受影响)
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms")