[C#] C#格式化日期和时间的代码片段 →→→→→进入此内容的聊天室

来自 , 2021-02-03, 写在 C#, 查看 141 次.
URL http://www.code666.cn/view/2dd75869
  1. using C = System.Console;
  2. ...
  3. static void Main() {
  4.    DateTime dateTime = DateTime.Now;
  5.    C.WriteLine ("d = {0:d}", dateTime );  // mm/dd/yyyy
  6.    C.WriteLine ("D = {0:D}", dateTime );  // month dd, yyyy
  7.    C.WriteLine ("f = {0:f}", dateTime );  // day, month dd, yyyy hh:mm
  8.    C.WriteLine ("F = {0:F}", dateTime );  // day, month dd, yyyy HH:mm:ss AM/PM
  9.    C.WriteLine ("g = {0:g}", dateTime );  // mm/dd/yyyy HH:mm
  10.    C.WriteLine ("G = {0:G}", dateTime );  // mm/dd/yyyy hh:mm:ss
  11.    C.WriteLine ("M = {0:M}", dateTime );  // month dd
  12.    C.WriteLine ("R = {0:R}", dateTime );  // ddd Month yyyy hh:mm:ss GMT
  13.    C.WriteLine ("s = {0:s}", dateTime );  // yyyy-mm-dd hh:mm:ss (Sortable)
  14.    C.WriteLine ("t = {0:t}", dateTime );  // hh:mm AM/PM
  15.    C.WriteLine ("T = {0:T}", dateTime );  // hh:mm:ss AM/PM
  16.  
  17.    // yyyy-mm-dd hh:mm:ss (Sortable)
  18.    C.WriteLine ("u = {0:u}", dateTime );  
  19.  
  20.    // day, month dd, yyyy hh:mm:ss AM/PM
  21.    C.WriteLine ("U = {0:U}", dateTime );
  22.  
  23.    // month, yyyy (March, 2006)
  24.    C.WriteLine ("Y = {0:Y}", dateTime );  
  25.    C.WriteLine ("Month = " + dateTime.Month); // month number (3)
  26.  
  27.    // day of week name (Friday)
  28.    C.WriteLine ("Day Of Week = " + dateTime.DayOfWeek);    
  29.  
  30.    // 24 hour time (16:12:11)
  31.    C.WriteLine ("Time Of Day = " + dateTime.TimeOfDay);    
  32.  
  33.    // (632769991310000000)
  34.    C.WriteLine("DateTime.Ticks = " + dateTime.Ticks);  
  35.    // Ticks are the number of 100 nanosecond intervals since 01/01/0001 12:00am
  36.    // Ticks are useful in elapsed time measurement.
  37.    }
  38.  
  39.  
  40.  
  41. //csharp/4159

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

这儿你可以回复上面这条便签

captcha