查询帮助获取确切的日期时差

时间:2012-04-20 10:00:59

标签: mysql

我有一个查询,从插入记录的时间到当前日期的时间和日期不同。

以下是示例。

select
datediff(date(now()),'2012-04-15 16:14:59') as DateDifference,
timediff(now(), '2012-04-15 16:14:59') as TimeDifference

但是这个给我以下结果:

  

日期差异时差   5 112:26:22

我真的希望得到两者之间的确切差异。

示例:

  

5 8:24:22

其中5是日,8:24:22是时间。

3 个答案:

答案 0 :(得分:0)

试试这个

SELECT floor((date1-date2)*24) || ' HOURS ' ||
mod(floor((date1-date2)*24*60),60) || ' MINUTES ' ||
mod(floor((date1-date2)*24*60*60),60) || ' SECS ' as time_difference
FROM dates;

答案 1 :(得分:0)

DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now.AddSeconds( 75 );
TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );

输出结果为:

Time Difference (seconds): 15
Time Difference (minutes): 1
Time Difference (hours): 0
Time Difference (days): 0

答案 2 :(得分:0)

尝试此查询:

select 
date(now()) date1, 
date('2012-04-15 16:14:59') date2,
datediff(date(now()),'2012-04-15 16:14:59') as DateDifference,
TIME(now()) time1, 
TIME('2012-04-15 16:14:59') time2, 
timediff(TIME(now()), TIME('2012-04-15 16:14:59')) as TimeDifference

这将为您提供准确的差异和比较。

希望这有帮助。