DateTime.Value.ToString(格式)给了我2小时时钟 [英] DateTime.Value.ToString(format) gives me 12 hour clock
问题描述
这个问题,我从数据库中提取日期和显示它。在网格
我有以下代码:
字符串日期=;
日期时间? dateSent;
如果(行[DateSent] = DBNull.Value!)
dateSent =(DateTime的)行[DateSent];
,否则dateSent = NULL;
日期=(dateSent.HasValue dateSent.Value.ToString(DD / MM / YYYY HH:MM:SS):零);
当我在这个代码块的末尾添加一个断点,我可以看到的DateTime?
变量dateSent有一个24小时制的时间戳如十四时50分34秒。然而,当我检查字符串的值
变量日期 - 它有一个12小时制的格式如二点五十分34秒
这不是我的本意转换为12小时时钟格式。我有两个问题:
- 为什么dateSent.Value.ToString(DD / MM / YYYY HH:MM:SS)返回2小时时钟时间戳?
- 我怎样才能避免这种情况,并使用24小时制的版本?
为什么dateSent.Value.ToString(DD / MM / YYYY HH:MM:SS)?返回12小时时钟时间戳
块引用>
由于你要求它。这就是
HH
表示。HH
24小时
当有疑问,请继续阅读的custom日期和时间格式字符串的。
HH
记录为
的小时,用从01到12的12小时制。
块引用>
HH
记录为:
的小时,使用00的24小时时钟23。
块引用>
作为进一步指出,该意见提出一个潜在的概念上的错误:
我可以看到,DateTime变量dateSent有一个24小时制?时间戳例如十四点50分34秒。
块引用>
A
的DateTime
值不有无的格式。这只是一个日期和时间,就像一个INT
有十六值是既为0x10和16视你如何格式化,同样是如此的DateTime
有没有格式,它与价值善有善报 - 只是调用的ToString()的结果
将取决于区域性设置。An extension of this question, 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 thestring
variable "date" - it has a 12-hour clock format eg 02:50:34.It is not my intention to convert to a 12 hour clock format. I have two questions:
- 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?
解决方案Why is dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") returning a 12 hour clock timestamp?
Because you're asking for it. That's what
hh
means.HH
is 24-hour.When in doubt, read the documentation on custom date and time format strings.
hh
is documented asThe hour, using a 12-hour clock from 01 to 12."
HH
is documented as:The hour, using a 24-hour clock from 00 to 23.
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
A
DateTime
value doesn't have a format. It's just a date and time. Just as anint
with a value of sixteen is both "0x10" and "16" depending on how you format it, the same is true forDateTime
. There's no format which goes around with the value - the results of just callingToString()
will depend on the culture settings.这篇关于DateTime.Value.ToString(格式)给了我2小时时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!