C# 查询日期时间关闭 sqlite 数据库失败 [英] C# query datetime off sqlite database fails

查看:27
本文介绍了C# 查询日期时间关闭 sqlite 数据库失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取一个 sqlite 文件,其中 datetime 列数据保存为整数值(INTEGER NO NULL)

I am reading a sqlite file in which datetime column data are saved as integer values (INTEGER NO NULL)

DateTime dt=reader.GetDateTime(nColDateTime);

但是它会发出一个错误,指出返回值的格式不正确.我在 Datetime 类中尝试了所有其他可用的方法,但只找到了

But it emits an error saying that the return value is not in correct format. I try out all other available methods in Datetime class and find only

DateTime dt=DateTime.FromBinary(reader.GetInt64(nColDateTime));

有效(因为其他人返回异常).但是格式化的日期(如 dt.ToShortDateTime())不正确(即 0042/11/20)我不知道这是什么.然后我试试这个

works (as others return exceptions). But the formatted date (as dt.ToShortDateTime()) is incorrect (ie 0042/11/20) I have no idea what this is. I then try this

long d=DateTime.Now.Ticks-reader.GetInt64(nColDateTime);
DateTime dt=new DateTime(d);

它给了我 1970/05/18

It gives me 1970/05/18

你能帮我获取正确的日期时间吗?

Could you help me to get the correct datetime ?

推荐答案

您的日期以 Unix 纪元格式存储.

Your dates are stored in the Unix epoch format.

您可能只想使用:

private static readonly DateTime epoch = new DateTime(1970, 1, 1);

...

var myDate = epoch + TimeSpan.FromTicks(reader.GetInt64(nColDateTime));


例如,当我查看上面1970/05/18"的示例时,我可以假设您的日期比今天早了大约 5 个月,也就是 18 天.


For example, when I look at your example above "1970/05/18", I can assume that your date is approximately 5 months, 18 days earlier than today.

这是我如何检索原始值:

Here is how I would retreieve the original value:

(DateTime.Today - new DateTime(1970, 5, 18)).Ticks

哪个返回:

13119840000000000

将其插入我的公式中:

new DateTime(1970, 1, 1) + TimeSpan.FromTicks(13119840000000000)

返回:

2011/07/30

这篇关于C# 查询日期时间关闭 sqlite 数据库失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆