如何获得当前时间(而不是日期和时间)?

例如:下午5:42:12


当前回答

非常简单的datetime,现在。ToString(“hh: mm: ss tt”)

其他回答

获取当前日期和时间,然后只使用它的时间部分。看看MSDN文档中格式化日期时间字符串的可能性。

非常简单的datetime,现在。ToString(“hh: mm: ss tt”)

带AM/PM指示器的当前时间:

DateTime.Now.ToString("hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)
DateTime.Now.ToString("hh:mm:ss.fff tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)

当前时间使用0-23小时记数法:

DateTime.Now.ToString("HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
DateTime.Now.ToString("HH:mm:ss.fff", System.Globalization.DateTimeFormatInfo.InvariantInfo)

使用String.Format()的另一个选项

string.Format("{0:HH:mm:ss tt}", DateTime.Now)

这个会好些,试试这个

    DateTime.Now.ToShortTimeString();

为此,您不需要指定时间的格式。