【问题标题】:asp.net - difference between today and other datesasp.net - 今天和其他日期之间的差异
【发布时间】:2013-01-11 16:20:19
【问题描述】:

我有一个 gridview,gridview 中的一列称为“最后检查”,这只是数据库中的一个日期时间字段。

在“上次检查”列中,我想要显示“7 天”或“1 小时”或“7 分钟”等,而不是显示日期和时间。

在我的 grdView_RowDataBound 子程序中(或者在 SQL 中,如果更简单的话)...有谁知道最简单的方法是什么?

谢谢,

【问题讨论】:

    标签: asp.net tsql


    【解决方案1】:

    如果您在 Sql-Server 上,可以使用 DATEDIFFCASE 从查询本身完成。

    假设您需要在超过一天时忽略小时数,如果距离 last_checked 日期时间超过一小时则忽略分钟数,您可以试试这个。

    --Some data to work with
    declare @t table ([last_checked] datetime)
    insert into @t ([last_checked]) values ('20130127 09:15:21'),('20130128 09:15:21'),('20130128 11:25:21')
    
    --Query
    select case when totMinutes >= 60*24 then 
                     convert(varchar(12), totMinutes/(60*24)) + ' day(s)'
                when totMinutes >= 60 then 
                     convert(varchar(12), totMinutes/(60)) + ' hour(s)'
           else convert(varchar(12), totMinutes) + ' min(s)' end timingCol
    from (
         select datediff(minute,[last_checked],getdate()) totMinutes
    from @t ) X
    

    【讨论】:

      【解决方案2】:

      好的,我想我可以在 SQL 中使用 CAST(DATEDIFF(mi, GETDATE(), GETDATE()+1) AS VARCHAR) 之类的东西来解决这个问题

      在几分钟内给我差异,然后使用 grdView_RowDataBound 子从那里获取差异

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-02
        • 2014-07-28
        • 2019-03-03
        相关资源
        最近更新 更多