如何将 sqlite 时间戳与 C# DateTime
进行比较?我有:
string query = string.Format(@"select avg(CPUH_VALUE_ALL) as Value from CPU_HOUR where CPUH_DATE >= @Date");
var pars = new List<SQLiteParameter>();
pars.Add(new SQLiteParameter("Date", DateTimeSQLite(DateTime.Now.AddMinutes(-12))));
在哪里
static string DateTimeSQLite(DateTime datetime)
{
string dateTimeFormat = "{0}-{1}-{2} {3}:{4}:{5}.{6}";
return string.Format(dateTimeFormat, datetime.Year, datetime.Month, datetime.Day, datetime.Hour, datetime.Minute, datetime.Second, datetime.Millisecond);
}
那是行不通的。当我尝试将时间戳与各种格式的日期字符串值进行比较时,也没有任何效果。有什么线索吗?
编辑
select CPUH_DATE from CPU_HOUR where CPUH_DATE
给我这样的记录:
5/19/2014 9:30:54 PM
编辑2 我刚刚发现虽然这不起作用:
select * from CPU_HOUR where CPUH_DATE >= '2014-5-19 21:30:08'
这是(注意 5 之前的零):
select * from CPU_HOUR where CPUH_DATE >= '2014-05-19 21:30:08'
有趣。
最佳答案
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS"). REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar. INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC. Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
来源:sqllite.org
我的建议是将日期时间值保存为整数。这样你在比较等操作上就不会有问题。如果您在查询中需要非数字日期,您可以像这样使用内置的 Datetime 函数:
SELECT datetime(1092941466, 'unixepoch');
关于c# - sqlite 在 C# 中比较时间戳和 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23745329/