Asp.Net 和 Sql Server 的时间差(以天、小时、分钟为单位)
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 .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.
使用以下查询的变体,并根据需要进行调整。对于其他 RDBMS,请将日期函数替换为适当的等效函数。
Use a variation on the following query, and adjust as needed. For other RDBMS's replace the date functions with appropriate equivalents.