C# 获取当前时间并格式化为 '2023年8月17日' - 代码示例
要获取当前时间并格式化为 '2023年8月17日' 的形式,可以使用 DateTime 类的 Now 属性获取当前时间,然后使用 ToString 方法指定日期格式,如下所示:
DateTime currentTime = DateTime.Now;
string formattedTime = currentTime.ToString('yyyy年M月d日');
Console.WriteLine(formattedTime);
输出结果:
2023年8月17日
在上述代码中,'yyyy年M月d日' 是日期格式的自定义字符串,其中:
- 'yyyy' 表示 4 位数的年份,如 2023;
- 'M' 表示月份,如 8;
- 'd' 表示日期,如 17。
你可以根据需要调整日期格式的自定义字符串。
原文地址: https://www.cveoy.top/t/topic/qmFL 著作权归作者所有。请勿转载和采集!