c#时间格式怪异

c#时间格式怪异,c#,C#,我将以下字符串时间格式作为自定义时间格式: "{0:h:mm tt}" 我有以下日期时间: {1/14/2011 2:19:00 AM} 我有以下声明 ThisTime = TheTime.ToString(TheTimeFormat); 为什么这个时间等于“{0:2:19 AM}”。应该是凌晨2点19分,我不知道为什么。并不是说在gridview中,我使用以下语句,它就像一个符咒: ((BoundField)(MyGrid.Columns[0])).DataFormatStr

我将以下字符串时间格式作为自定义时间格式:

  "{0:h:mm tt}"
我有以下日期时间:

  {1/14/2011 2:19:00 AM}
我有以下声明

  ThisTime = TheTime.ToString(TheTimeFormat);
为什么这个时间等于“
{0:2:19 AM}
”。应该是凌晨2点19分,我不知道为什么。并不是说在gridview中,我使用以下语句,它就像一个符咒:

((BoundField)(MyGrid.Columns[0])).DataFormatString=TheUserPreferences.UserTimeFormat
;其中UserPreferences.UserTimeFormat是相同的格式字符串


有什么建议吗?

当您将时间格式放入
DateTime
ToString
方法时,您不需要占位符;也就是说,你可以这样做:

TheTimeFormat=“h:mm tt”

thisttime=time.ToString(timeformat)

将为您提供所需的输出

编辑:

根据注释中的扩展问题,这将允许您将格式放在需要占位符的位置(并将上述版本的TimeFormat存储在db中):

字符串formatWithPlaceholder=“{0:+TheTimeFormat+”}”


我只是简单地使用了
string.Format
而不是concatentation,但是在格式中使用大括号会带来问题。尽管如此,这是可行的,但我并不特别喜欢串联。

将时间格式更改为简单的
“0:h:mm tt”


当您在内部使用
String.format()
或其他方法(如
Console.WriteLine()
)时,只需要“{X:YYY}”格式。

ah ok;那么在我的gridview里呢?在数据库中,我将字符串“{0:h:mmtt}”存储为用户的时间格式;如果将数据库更改为“h:mm tt”,我需要对语句((BoundField)(MyGrid.Columns[0])执行什么操作。DataFormatString=TheUserPreferences.UserTimeFormat?