在C#中,可以使用DateTime对象的ToString方法来格式化日期时间。以下是一些常用的日期时间格式化样式:
示例代码:
DateTime dt = DateTime.Now; string formattedDate = dt.ToString("yyyyMMdd"); Console.WriteLine(formattedDate); string formattedDateTime = dt.ToString("yyyy-MM-dd HH:mm:ss"); Console.WriteLine(formattedDateTime); string formattedDateLong = dt.ToString("MMM dd, yyyy"); Console.WriteLine(formattedDateLong); string formattedTime = dt.ToString("HH:mm:ss"); Console.WriteLine(formattedTime); string formattedDateTimeFull = dt.ToString("dddd, MMMM dd, yyyy HH:mm:ss"); Console.WriteLine(formattedDateTimeFull); 以上是一些常用的日期时间格式化样式,您可以根据需要选择合适的格式化样式来格式化日期时间。