您不能用%Y-%m-%d ...
表示時間差,至少不是日期格式。你如何表達不到一天的差異? (0000-00-00 ...
不是有效日期)。另外,一個月會是什麼? 30天? 31天? 23423432天?
我建議你在幾秒鐘內保持你的區別,並在展示它時根據需要進行調整。
在另一方面,如果你真的想這樣做,你問,這裏有一個方法:
sqlite> select datetime(strftime('%s','2012-01-01 12:00:00')
- strftime('%s','2004-01-01 02:34:56') - 62167305600, 'unixepoch');
0007-12-31 09:25:04
即使我覺得由OP的downvote是沒有道理的,我無法阻止自己從解釋爲什麼我上面提到的顯然不是一個非常好的選項,當時差小於1天時返回「不正確」結果:原因暗示在我上面寫的內容中:沒有像0000-00-00 ...
這樣的日期,而是日期時間退回到負數區域:-001-12-31 ...
以下是一種獲取438:53:45
,但它是相當複雜:
earlier date: d1
later date: d2
select
cast(
(strftime('%s', d2) - strftime('%s', d1))/86400 * 24
+ cast(strftime("%H", time(strftime('%s', d2)
- strftime('%s', d1), 'unixepoch'))
as int)
as text)
|| ":"
|| substr(time(strftime('%s', d2) - strftime('%s', d1), 'unixepoch'), 4);
例子:
d1 = '2004-01-01 02:34:56'
d2 = '2012-01-01 12:00:00'
sqlite> select cast((strftime('%s','2012-01-01 12:00:00') - strftime('%s','2004-01-01 02:34:56'))/86400 *24 + cast(strftime("%H", time(strftime('%s','2012-01-01 12:00:00') - strftime('%s','2004-01-01 02:34:56'), 'unixepoch')) as int) as text)
|| ":"
|| substr(time(strftime('%s','2012-01-01 12:00:00') - strftime('%s','2004-01-01 02:34:56'), 'unixepoch'), 4);
70137:25:04
這是不好的 '選擇日期時間(的strftime( '%s' 的, '2004-01-01 12:00:00') - strftime('%s','2004-01-01 02:34:56') - 62167305600,'unixepoch');''''-001-12-31 09:25:04''。但我怎麼能得到像MySQL輸出'select timediff(now(),'2012-07-01 12:00:00')' - >''438:53:45'? – microo8 2012-07-19 10:42:06
只是可以肯定,你沒有倒下,對吧? – 2012-07-19 10:43:10
是的,我已經低估了,答案是不正確的。 – microo8 2012-07-19 10:48:05