Asp.Net 和 Sql Server 的时间差(以天、小时、分钟为单位)

发布于 2024-08-15 00:54:40 字数 365 浏览 6 评论 0原文

我正在使用 asp.net 和 sql server 2005 开发一个问题跟踪应用程序。在跟踪问题时,我将截止日期作为日期时间存储在数据库中,并且如果有人在留言板中发布消息,发布日期/时间也会得到作为日期时间存储在数据库中。

现在我的要求是,当只剩下2天时,将问题截止日期显示为“还剩2天”,就像截止日期已经过了时,会显示比计划晚了2天。我认为这可以通过 sql server datediff() 来实现,但是如果我需要显示消息,例如 2 小时 3 分钟前发布的消息......或 3 天 18 小时前发布的消息或类似的消息......(如果是在 1 天前发布的,我们可以取消这些时间)。

尝试谷歌但找不到任何有用的东西。会;感谢您一如既往的善意帮助。

提前致谢。

I am working on a issue tracking application with asp.net and sql server 2005. While tracking the issues I am storing the deadline as datetime in the database and also if someone posts a message in the message board the posting date/time is also getting stored in the db as datetime.

Now my requirement is to show the issue deadline as "2 days left" when only 2 days are left, just like that when the deadline is already over it sud display 2 days behind schedule. This I think could be achieved with sql server datediff(), but in case of the messages I need to show like posted by 2 hours 3 minutes ago....or posted by 3 days 18 hours ago or something similar...(we can eliminate the hours if it was posted even 1 day before).

Tried Google but could not find anything useful. Would; appreciate your kind help as always.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

著墨染雨君画夕 2024-08-22 00:54:40

您可以使用 .NET TimeSpan 结构来获取此功能。

您可以通过从另一个日期减去一个日期来得到它,它给出您需要的小时/分钟/秒详细信息。

Instead of doing this in SQL, you can use the .NET TimeSpan struct to get this functionality.

You can get it by subtracting one date from another, and it gives you the hours/minutes/seconds details you need.

风流物 2024-08-22 00:54:40

使用以下查询的变体,并根据需要进行调整。对于其他 RDBMS,请将日期函数替换为适当的等效函数。

SELECT  CASE
          WHEN DateValue-GETDATE() >= 1 THEN CAST(DATEPART(dy, DateValue-GETDATE()) AS varchar) + ' day(s) remaining'
          WHEN DateValue-GETDATE() >= 0 THEN CAST(DATEPART(hh, DateValue-GETDATE()) AS varchar) + ' hour(s) remaining'
          /*Note, it's convenient to switch around for negatives*/
          WHEN GETDATE()-DateValue <= '00:59' THEN CAST(DATEPART(mi, GETDATE()-DateValue) AS varchar) + ' minutes(s) overdue'
          WHEN GETDATE()-DateValue <= 1 THEN CAST(DATEPART(hh, GETDATE()-DateValue) AS varchar) + ' hours(s) overdue'
        ELSE
          CONVERT(varchar, DateValue-GETDATE(), 121)
        END AS Time_Scale

Use a variation on the following query, and adjust as needed. For other RDBMS's replace the date functions with appropriate equivalents.

SELECT  CASE
          WHEN DateValue-GETDATE() >= 1 THEN CAST(DATEPART(dy, DateValue-GETDATE()) AS varchar) + ' day(s) remaining'
          WHEN DateValue-GETDATE() >= 0 THEN CAST(DATEPART(hh, DateValue-GETDATE()) AS varchar) + ' hour(s) remaining'
          /*Note, it's convenient to switch around for negatives*/
          WHEN GETDATE()-DateValue <= '00:59' THEN CAST(DATEPART(mi, GETDATE()-DateValue) AS varchar) + ' minutes(s) overdue'
          WHEN GETDATE()-DateValue <= 1 THEN CAST(DATEPART(hh, GETDATE()-DateValue) AS varchar) + ' hours(s) overdue'
        ELSE
          CONVERT(varchar, DateValue-GETDATE(), 121)
        END AS Time_Scale
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文