• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#格式化日期和时间的代码片段

OC/C/C++ 水墨上仙 2161次浏览

下面的c#代码片段使用不同的格式显示日期和时间。

using C = System.Console; 
...
static void Main() {
   DateTime dateTime = DateTime.Now;
   C.WriteLine ("d = {0:d}", dateTime );  // mm/dd/yyyy
   C.WriteLine ("D = {0:D}", dateTime );  // month dd, yyyy
   C.WriteLine ("f = {0:f}", dateTime );  // day, month dd, yyyy hh:mm 
   C.WriteLine ("F = {0:F}", dateTime );  // day, month dd, yyyy HH:mm:ss AM/PM 
   C.WriteLine ("g = {0:g}", dateTime );  // mm/dd/yyyy HH:mm
   C.WriteLine ("G = {0:G}", dateTime );  // mm/dd/yyyy hh:mm:ss
   C.WriteLine ("M = {0:M}", dateTime );  // month dd
   C.WriteLine ("R = {0:R}", dateTime );  // ddd Month yyyy hh:mm:ss GMT
   C.WriteLine ("s = {0:s}", dateTime );  // yyyy-mm-dd hh:mm:ss (Sortable)
   C.WriteLine ("t = {0:t}", dateTime );  // hh:mm AM/PM
   C.WriteLine ("T = {0:T}", dateTime );  // hh:mm:ss AM/PM
 
   // yyyy-mm-dd hh:mm:ss (Sortable)
   C.WriteLine ("u = {0:u}", dateTime );  
 
   // day, month dd, yyyy hh:mm:ss AM/PM
   C.WriteLine ("U = {0:U}", dateTime );
 
   // month, yyyy (March, 2006)
   C.WriteLine ("Y = {0:Y}", dateTime );  
   C.WriteLine ("Month = " + dateTime.Month); // month number (3)
 
   // day of week name (Friday)
   C.WriteLine ("Day Of Week = " + dateTime.DayOfWeek);     
 
   // 24 hour time (16:12:11)
   C.WriteLine ("Time Of Day = " + dateTime.TimeOfDay);     
 
   // (632769991310000000)
   C.WriteLine("DateTime.Ticks = " + dateTime.Ticks);   
   // Ticks are the number of 100 nanosecond intervals since 01/01/0001 12:00am
   // Ticks are useful in elapsed time measurement.
   }

输出结果如下

d = 3/3/2006
D = Friday, March 03, 2006
f = Friday, March 03, 2006 4:20 PM
F = Friday, March 03, 2006 4:20:26 PM
g = 3/3/2006 4:20 PM
G = 3/3/2006 4:20:26 PM
M = March 03
R = Fri, 03 Mar 2006 16:20:26 GMT
s = 2006-03-03T16:20:26
t = 4:20 PM
T = 4:20:26 PM
u = 2006-03-03 16:20:26Z
U = Friday, March 03, 2006 10:20:26 PM
Y = March, 2006
Month = 3
Day Of Week = Friday
Time Of Day = 16:20:26.1406250
DateTime.Ticks = 632769996261406250


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#格式化日期和时间的代码片段
喜欢 (0)
加载中……