.net DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") 返回 AM 时间而不是 PM 时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8128383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") is returning AM time instead of PM time?
提问by Oded
I'm trying to get the current time via DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")
我正在尝试通过以下方式获取当前时间 DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")
However, this is spitting out a time 12 hours off of what we want.
但是,这与我们想要的时间相差 12 小时。
For example:
例如:
What it spits out: 11/14/2011 2:24:56 am
它吐出的是什么: 11/14/2011 2:24:56 am
What we want: 11/14/2011 2:24:56 pm
我们想要的是: 11/14/2011 2:24:56 pm
What noob mistake are we making?
我们犯了什么菜鸟错误?
Any help is greatly appreciated :)
任何帮助是极大的赞赏 :)
回答by Oded
Use HHfor 24 hour hours format:
使用HH24 小时制:
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
Or the ttformat specifier for the AM/PMpart:
或部件的tt格式说明符AM/PM:
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt")
Take a look at the custom Date and Time format stringsdocumentation.
查看自定义日期和时间格式字符串文档。
回答by Callum
With C#6.0 you also have a new way of formatting date when using string interpolation e.g.
使用 C#6.0,当使用字符串插值时,您还有一种新的日期格式设置方法,例如
$"{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.
不能说它更好,但如果将格式化的 DateTime 包含在更长的字符串中,它会稍微干净一些。

