本文介绍了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小时制的版本?


解决方案

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

HH is documented as:

As a further note, this comment suggests a potential conceptual error:

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小时时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:05