Need to format timespan ADO.NET datacolumn as AM/PM

后端 未结 1 1825
说谎
说谎 2021-01-03 11:34

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 i

相关标签:
1条回答
  • 2021-01-03 12:20

    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.

    0 讨论(0)
提交回复
热议问题