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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:08:14  来源:igfitidea点击:

DateTime.Value.ToString(format) gives me 12 hour clock

c#

提问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 stringvariable "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 小时制。我有两个问题:

  1. Why is dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") returning a 12 hour clock timestamp?
  2. How can I avoid this and use the 24-hour clock version?
  1. 为什么 dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") 返回 12 小时时钟时间戳?
  2. 如何避免这种情况并使用 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 hhmeans. HHis 24-hour.

因为你要求它。就是这个hh意思。HH是 24 小时制。

When in doubt, read the documentation on custom date and time format strings.

如有疑问,请阅读有关自定义日期和时间格式字符串的文档。

hhis documented as

hh记录为

The hour, using a 12-hour clock from 01 to 12."

小时,使用从 01 到 12 的 12 小时制。”

HHis 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 DateTimevalue doesn't havea format. It's just a date and time. Just as an intwith 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

希望能帮助到你