c# - sqlite 在 C# 中比较时间戳和 DateTime

标签 c# sqlite datetime

如何将 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/

相关文章:

C# 简化并可能概括了我的对象克隆方法

database - 我的 Win JS SQLite 3 应用程序需要太多时间来进行插入

c# - AppFabric 缓存服务器和 Web 应用程序在同一台物理机器上

c# - Sitecore LinkManager GetItemUrl。为什么这么棘手?

java - 将记录从数组插入到 Sqlite 数据库 (Android)

python - 计算到年底python的时间

sql - 在 TSQL 中检查两个日期时间是否在同一日历日的好方法是什么?

java - SimpleDateFormat 用 'Z' 文字解析日期

c# - MassTransit 和简易喷油器

ruby - SQLite 是在 Heroku 上备份 postgres 的好解决方案吗?