C# DateTime.Value.ToString(format) 给了我 12 小时制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15837609/
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.Value.ToString(format) gives me 12 hour clock
提问by Mike Baxter
An extension of thisquestion, I am pulling a date from a database and displaying it in a grid.
这个问题的扩展,我从数据库中提取日期并将其显示在网格中。
I have the following code:
我有以下代码:
string date = "";
DateTime? dateSent;
if (row["DateSent"] != DBNull.Value)
dateSent = (DateTime)row["DateSent"];
else dateSent = null;
date = (dateSent.HasValue ? dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") : null);
When I add a breakpoint at the end of this block of code, I can see that the DateTime?
variable "dateSent" has a 24-hour clock timestamp eg 14:50:34. However, when I check the value of the string
variable "date" - it has a 12-hour clock format eg 02:50:34.
当我在这段代码的末尾添加断点时,我可以看到DateTime?
变量“dateSent”有一个 24 小时制的时间戳,例如 14:50:34。但是,当我检查string
变量“日期”的值时- 它具有 12 小时制格式,例如 02:50:34。
It is not my intention to convert to a 12 hour clock format. I have two questions:
我无意转换为 12 小时制。我有两个问题:
- Why is dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") returning a 12 hour clock timestamp?
- How can I avoid this and use the 24-hour clock version?
- 为什么 dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") 返回 12 小时时钟时间戳?
- 如何避免这种情况并使用 24 小时制版本?
采纳答案by Jon Skeet
Why is dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") returning a 12 hour clock timestamp?
为什么 dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") 返回 12 小时时钟时间戳?
Because you're asking for it. That's what hh
means. HH
is 24-hour.
因为你要求它。就是这个hh
意思。HH
是 24 小时制。
When in doubt, read the documentation on custom date and time format strings.
如有疑问,请阅读有关自定义日期和时间格式字符串的文档。
hh
is documented as
hh
记录为
The hour, using a 12-hour clock from 01 to 12."
小时,使用从 01 到 12 的 12 小时制。”
HH
is documented as:
HH
记录为:
The hour, using a 24-hour clock from 00 to 23.
小时,使用从 00 到 23 的 24 小时制。
As a further note, this comment suggests a potential conceptual error:
进一步说明,此评论暗示了一个潜在的概念错误:
I can see that the DateTime? variable "dateSent" has a 24-hour clock timestamp eg 14:50:34
我可以看到DateTime?变量“dateSent”有一个 24 小时制的时间戳,例如 14:50:34
A DateTime
value doesn't havea format. It's just a date and time. Just as an int
with a value of sixteen is both "0x10" and "16" depending on how you format it, the same is true for DateTime
. There's no format which goes around with the value - the results of just calling ToString()
will depend on the culture settings.
一个DateTime
值不具有的格式。这只是一个日期和时间。就像int
值为 16 的an既是“0x10”又是“16”,这取决于您如何对其进行格式化,对于DateTime
. 没有与值相关的格式 - 调用的结果ToString()
将取决于文化设置。
回答by Robert Harvey
Use capital letters for the Hours:
使用大写字母表示小时:
HH:mm:ss
^^
回答by Justin Pihony
It should be as simple as capitalizing your h's
它应该像大写你的 h 一样简单
HH:mm:ss
Here is a decent link to string formating date times
From the article:
从文章:
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
Notice that the hour is 16
请注意,小时是 16
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
回答by Daniel Pe?alba
If you want to display a 24-hours string use the following format string:
如果要显示 24 小时字符串,请使用以下格式字符串:
HH:mm:ss
Hope it helps
希望能帮助到你