c# datetime.now显示小时分钟秒毫秒
时间: 2023-11-12 19:05:37
浏览: 201

用C#编写的显示当前时间的程序
要在 C# 中显示当前时间的小时、分钟、秒和毫秒数,可以使用 `DateTime.Now` 方法获取当前的日期和时间,然后使用 `.To[string](https://wenku.csdn.net/doc/645cade695996c03ac3eb2ce?spm=1055.2569.3001.10083)()` 方法指定日期时间的格式。下面是示例代码:
```c[sharp](https://wenku.csdn.net/doc/4mha40i4h2?spm=1055.2569.3001.10083)
DateTime now = DateTime.Now;
string [format](https://wenku.csdn.net/doc/645b7ad7fcc53913682b861a?spm=1055.2569.3001.10083)tedDateTime = now.[tostring](https://wenku.csdn.net/doc/5m0ig35y3b?spm=1055.2569.3001.10083)("HH:mm:ss.fff");
Console.[write](https://wenku.csdn.net/doc/6412b4acbe7fbd1778d406ac?spm=1055.2569.3001.10083)Line($"当前时间为:{formattedDateTime}");
输出结果类似于:`当前时间为:09:30:15.123`。
在上述代码中,`"HH:mm:ss.fff"` 是指定日期时间格式的字符串,其中 `HH` 表示小时数(24小时制),`mm` 表示分钟数,`ss` 表示秒数,`fff` 表示毫秒数。你可以根据需要调整日期时间格式的字符串来显示不同的时间信息。
阅读全文
相关推荐















