1 /// <summary> 2 /// 时间格式转换 3 /// </summary> 4 public static void DateTimeToString() 5 { 6 DateTime dt = DateTime.Now; 7 Console.WriteLine(dt); 8 Console.WriteLine(dt.ToString("yyyy-M-dd h:m:ss")); //12小时制 9 Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:s")); //24小时制 10 Console.WriteLine(dt.ToString("yyyy-M-d")); //月份前带0 11 Console.WriteLine(dt.ToString("yyyy-MM-dd")); //月份前无0 12 }