ToString (“ yyyy-MM-dd hh: mm: ss”)返回的是 AM 时间而不是 PM 时间?

我试图通过 DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")得到当前时间

但是,这比我们想要的时间少了12个小时。

例如:

它吐出的东西: 11/14/2011 2:24:56 am

我们想要的: 11/14/2011 2:24:56 pm

我们在犯什么菜鸟错误?

非常感谢您的任何帮助:)

374968 次浏览

Use HH for 24 hour hours format:

DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")

Or the tt format specifier for the AM/PM part:

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt")

Take a look at the custom Date and Time format strings documentation.

With C#6.0 you also have a new way of formatting date when using string interpolation e.g.

$"{DateTime.Now:yyyy-MM-dd HH:mm:ss}"

Can't say its any better, but it is slightly cleaner if including the formatted DateTime in a longer string.

More about string interpolation.