在C#中使用String.Format方法处理日期和时间可以通过以下方式:
使用标准的日期和时间格式字符串:DateTime now = DateTime.Now;string formattedDate = string.Format("Today is {0:MM/dd/yyyy}", now);string formattedTime = string.Format("The current time is {0:HH:mm:ss}", now);使用自定义的日期和时间格式字符串:DateTime now = DateTime.Now;string formattedDateTime = string.Format("The current date and time is {0:yyyy-MM-dd HH:mm:ss}", now);使用DateTime.ToString方法:DateTime now = DateTime.Now;string formattedDate = now.ToString("MM/dd/yyyy");string formattedTime = now.ToString("HH:mm:ss");string formattedDateTime = now.ToString("yyyy-MM-dd HH:mm:ss");无论使用哪种方式,都可以通过指定不同的日期和时间格式字符串来格式化输出的日期和时间。


