需要将时间跨度 ADO.NET 数据列格式化为 AM/PM

Need to format timespan ADO.NET datacolumn as AM/PM(需要将时间跨度 ADO.NET 数据列格式化为 AM/PM)
本文介绍了需要将时间跨度 ADO.NET 数据列格式化为 AM/PM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataColulmn TimeSpan 类型的 DataTable.它在 SQL Server 查询中填充了一个时间列,并在屏幕上显示为 24 小时格式.我想使用表达式列将其显示为 12 小时格式 + 上午/下午,但无法做到.我尝试使用 convert to the column 来处理字符串,但它显示了一种奇怪的格式,例如 PT20H15(即 20:15 或 8:15 pm).如果小时为一位或两位数,则时间和小时的位置会发生变化,如果小时为 00,则将其删除,字符串处理似乎太复杂了.我尝试仅比较HourCol > 12"之类的列值,但这会引发关于将时间跨度与 int 进行比较的错误,因此我再次失败.有没有人有不同的建议?

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?

提前致谢.

推荐答案

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

it shows a strange format such as PT20H15

这根本不是一种奇怪的格式.它是一段时间的 ISO-8601 格式,对于 TimeSpan 来说是相当合理的.

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

我想将其显示为 12 小时格式 + 上午/下午

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

听起来您正在使用 TimeSpan 来表示一天中的时间",说实话这远非理想.我知道这是 DateTime.TimeOfDay 给出的,但这只是因为框架没有仅时间"类型,运气不好.

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.

有人有不同的建议吗?

你能否有一个计算列,它基本上是一些固定日期(甚至是 DateTime.Today)+ 时间跨度值,并使用 hh:mm tt 格式化格式?(您是否可以自己手动覆盖 DataTable 中的值,而不是使用计算表达式?)

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?)

另一种选择是更改您的 SQL 查询以转换为日期/时间值,而不是在 DataTable 中进行.

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.

这篇关于需要将时间跨度 ADO.NET 数据列格式化为 AM/PM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)