Database : SQLite
Column : SomeTable.Logged (DateTime)
我正在使用System.Data.SQLite
组件。我将日期时间值保存在Logged
列中作为刻度。例如使用c#
DateTime.Now.Ticks;
SomeTable.Logged
中保存的示例值为:
634399267463299880
如何使用sql在“正常”日期显示?例如'01-05-2011 13:45:22'
?
我知道页面http://www.sqlite.org/lang_datefunc.html,但我无法按照我想要的方式工作。
答案 0 :(得分:21)
尝试:
SELECT strftime('%Y-%m-%d %H:%M:%S',
SomeTable.Logged/10000000 - 62135596800,
'unixepoch')
<强> ,其中 强>:
62135596800 = DateTime(1970, 1, 1, 0, 0, 0).Ticks/10000000
= number of seconds elapsed from 01/01/0001 00:00:00
until 01/01/1970 00:00:00;
=> SomeTable.Logged/10000000 - 62135596800
= number of seconds elapsed from 01/01/1970 00:00:00
until your date
=> 'unixepoch' to convert it to date value
=> '%Y-%m-%d %H:%M:%S' to format
示例:
SELECT strftime('%Y-%m-%d %H:%M:%S',
634398543220000000/10000000 - 62135596800,
'unixepoch')
==> 2011-05-01 13:45:22