DateTime.Value.ToString(格式)给了我2小时时钟 [英] DateTime.Value.ToString(format) gives me 12 hour clock

查看:452
本文介绍了DateTime.Value.ToString(格式)给了我2小时时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题,我从数据库中提取日期和显示它。在网格



我有以下代码:

 字符串日期=; 
日期时间? 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秒

$ B $。 b

这不是我的本意转换为12小时时钟格式。我有两个问题:




  1. 为什么dateSent.Value.ToString(DD / MM / YYYY HH:MM:SS)返回2小时时钟时间戳?

  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 the string 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:

  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?

解决方案

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 as

The 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 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.Value.ToString(格式)给了我2小时时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆