两小时之间的差异以小时为单位

时间:2016-07-07 12:29:53

标签: c# asp.net oracle

我在unix时间的数据库中有两个日期。我想要它们之间的区别,并在几小时内显示出来。

例如
Date1:​​05/07/2016 14:40:00 PM (1467733205)
Date2:05/07/2016 15:40:05 PM (1467729600)

差: Date2 - Date1 = 3605 s

结果我想在几个小时内告诉他。 - > 1小时5秒

    SELECT TO_CHAR(to_date('01/01/1970', 'dd/mm/yyyy') + (((HOUR_DEPARTURE - DATE_LOADING )/ 60 / 60 / 24)), 'dd "days and" HH:MI:SS')
  FROM dual

我尝试过这样但是在01天和01:00:05都很糟糕。没有一天的差异。

3 个答案:

答案 0 :(得分:0)

您的解决方案中的dd从1970年1月1日起选择该月的某一天。所以你已经开始了1天。你不能简单地减1来得到“0天”,因为计算是在日历日期完成的,而不是“天数”。

如果你想在几天内得到答案,那么......:mi:ss那么你可以这样做:

import React, { Component } from 'react';
import RectDOM from 'react-dom';
import Login from './Login';

export default class Logout extends React.Component { ...

答案 1 :(得分:0)

SELECT    DECODE (d,  0, NULL,  1, '1 day ',  TO_CHAR (d) || ' days ')
       || DECODE (h,  0, NULL,  1, '1 hour ',  TO_CHAR (h) || ' hours ')
       || DECODE (m,  0, NULL,  1, '1 minute ',  TO_CHAR (m) || ' minutes ')
       || DECODE (s,  0, NULL,  1, '1 second',  TO_CHAR (s) || ' seconds')
  FROM (SELECT EXTRACT (DAY FROM (dt)) d, EXTRACT (HOUR FROM (dt)) h, EXTRACT (MINUTE FROM (dt)) m, EXTRACT (SECOND FROM (dt)) s
          FROM (SELECT NUMTODSINTERVAL (:HOUR_DEPARTURE - :DATE_LOADING, 'DAY') AS dt FROM DUAL))

为了好玩:

SELECT    DECODE (y,  0, NULL,  1, '1 year ',  TO_CHAR (y) || ' years ') 
       || DECODE (m,  0, NULL,  1, '1 month ',  TO_CHAR (m) || ' months ') 
       || DECODE (d,  0, NULL,  1, '1 day ',  TO_CHAR (d) || ' days ')
       || DECODE (h,  0, NULL,  1, '1 hour ',  TO_CHAR (h) || ' hours ')
       || DECODE (mi,  0, NULL,  1, '1 minute ',  TO_CHAR (mi) || ' minutes ')
       || DECODE (s,  0, NULL,  1, '1 second',  TO_CHAR (s) || ' seconds')
  FROM (SELECT EXTRACT (YEAR FROM (ym)) y,EXTRACT (MONTH FROM (ym)) m, 
               EXTRACT (DAY FROM (dt)) d, EXTRACT (HOUR FROM (dt)) h, EXTRACT (MINUTE FROM (dt)) mi, EXTRACT (SECOND FROM (dt)) s
          FROM (SELECT NUMTOYMINTERVAL (MONTHS_BETWEEN (:HOUR_DEPARTURE, :DATE_LOADING), 'MONTH') AS ym ,
                       NUMTODSINTERVAL (ADD_MONTHS(:HOUR_DEPARTURE,-MONTHS_BETWEEN (:HOUR_DEPARTURE, :DATE_LOADING)) - :DATE_LOADING, 'DAY') AS dt 
                  FROM DUAL
                )
        )

答案 2 :(得分:0)

Oracle安装程序

CREATE TABLE your_table ( date1, date2 ) AS
SELECT 1467733205, 1467729600 FROM DUAL;

<强>查询

SELECT TRUNC( diff ) AS days,
       TRUNC( MOD( diff * 24, 24 ) ) AS hours,
       TRUNC( MOD( diff * 24*60, 60 ) ) AS minutes,
       ROUND( MOD( diff * 24*60*60, 60 ) ) AS seconds
FROM   (
  SELECT ( DATE '1970-01-01' + date1 /24/60/60 )
         - ( DATE '1970-01-01' + date2 /24/60/60 ) AS diff
  FROM   your_table
);

<强>输出

DAYS HOURS MINUTES SECONDS
---- ----- ------- -------
   0     1       0       5

查询2

SELECT TRIM(
         REGEXP_REPLACE(
           REGEXP_REPLACE(
             (  ( DATE '1970-01-01' + date1 / 24/60/60 )
              - ( DATE '1970-01-01' + date2 / 24/60/60 ) ) DAY TO SECOND,
             '^\+?(-?)0*(\d+) 0?(\d+):0?(\d+):0?(\d+)\.0+$'
             '\1\2 days \3 hours \4 minutes \5 seconds'
           ),
           '(^| )0 (days|hours|minutes|seconds)'
         )
       ) AS difference
FROM   your_table

<强>输出

DIFFERENCE
-----------------
1 hours 5 seconds