2011-05-02 83 views
3
Database : SQLite 
Column : SomeTable.Logged (DateTime) 

Im使用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,但我不能讓事情按我想要的方式工作。

回答

19

嘗試:

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 
+0

你知道,如果SQLLite.net它們存儲爲蜱能夠使用DateTimeOffsets和DateTime?或者它是一個表演的東西? – 2015-07-15 10:31:52

相關問題