需要格式化时间跨度ADO.NET的DataColumn为AM / PM跨度、时间、ADO、NET

2023-09-03 02:16:09 作者:各种虚伪各种装

我有DataColulmn时间跨度的类型的DataTable。它被填充在SQL Server查询时间列,它显示为屏幕上的24小时格式。我想它显示为12小时格式+ AM / PM使用EX pression列,但coulnd't做到这一点。 我试着用转换为该列来处理字符串,但它显示了一个奇怪的格式,如PT20H15(这将是20:15或8:15 PM)。随着时间和时间变化的位置,如果小时为一个或两个数字,如果小时为00则得到去除,字符串处理好像太复杂了。我尝试过比较喜欢HourCol> 12的列值,但也带来新的比较时间跨度为int的错误,所以我再次失败。有没有人有不同的意见吗?

在此先感谢。

解决方案   

它显示了一个奇怪的格式,例如PT20H15

这不是一个奇怪的格式在所有。这是为期一个ISO-8601格式,这是一个时间跨度

还算合理   

我想它显示为12小时格式+ AM / PM

这听起来像您使用的是时间跨度的一天中的时间,这是很不理想的是诚实的。我知道这是什么 DateTime.TimeOfDay 给出,但这只是因为框架没有一个时间只型,更糟糕的运气。

  

有没有人有不同的意见吗?

您可以拥有使用 HH:MM TT 格式? (你可以手动覆盖值在自己的数据表,也许,而不是使用一个计算EX pression?)

另一种方法是改变你的SQL查询来执行转换为日期/时间值,而不是做在数据表

A站要复活 延长域名过期时间并发布多条招聘信息

I have a DataTable with a DataColulmn of TimeSpan type. It gets filled with a time column in a SQL Server query, and it shows as a 24 hr format on screen. I wanted to show it as a 12 hr format + am/pm using an expression column, but coulnd't do it. I tried using convert to the column to handle the string, but it shows a strange format such as PT20H15 (that would be 20:15 or 8:15 pm). As the position of time and hour changes if the hour is one or two digits, and if hour is 00 then it get removed, string handling seems like too complicated. I tried just comparing the column value like "HourCol > 12" but that raises an error regarding comparing timespan to int, so I failed again. Does anyone have a different suggestion?

Thanks in advance.

解决方案

it shows a strange format such as PT20H15

That's not a strange format at all. It's an ISO-8601 format for a period, which is fairly reasonable for a TimeSpan.

I wanted to show it as a 12 hr format + am/pm

That sounds like you're using a TimeSpan for a "time of day", which is far from ideal to be honest. I know it's what DateTime.TimeOfDay gives, but that's just because the framework doesn't have a "time only" type, worse luck.

Does anyone have a different suggestion?

Can you have a computed column which is basically some fixed date (or even DateTime.Today) + the timespan value, and format that using a hh:mm tt format? (Could you manually overwrite the values yourself in the DataTable, perhaps, rather than using a computed expression?)

Another alternative would be to change your SQL query to do the conversion to a date/time value, rather than doing it in the DataTable.