如何比较sql datetime和c# datetime

时间:2021-05-14 08:52:18

This was born from my previous question

这是我之前的问题

I have a DateTime in c#.

我在c#中有一个DateTime。

Then this value is inserted to database.

然后将该值插入数据库。

After that select this value and compare that date is the same as it was in the beginning.

在此之后,选择这个值并比较该日期与开始时相同。

What is the best way to do this? Since SQL datetime has different ticks, DateTime from the first step will not be the same as SQL DateTime (row["MyDate"])

最好的方法是什么?由于SQL datetime有不同的滴答,所以从第一步开始的datetime将不与SQL datetime (row["MyDate"])相同。

How to compare them?

如何进行比较?

3 个解决方案

#1


2  

You can use the SqlDateTime structure.

您可以使用SqlDateTime结构。

DateTime now = DateTime.Now;
SqlDateTime sqlNow = new SqlDateTime(now);
bool equal = now == sqlNow.Value; // false

So if you have a DateTime and want to know if it's equal to a DB-DateTime use:

如果你有一个DateTime,想知道它是否等于DB-DateTime:

Assert.Equal(dbEndTime, new SqlDateTime(endTime).Value); //  true

SqlDateTime:

SqlDateTime:

Represents the date and time data ranging in value from January 1, 1753 to December 31, 9999 to an accuracy of 3.33 milliseconds to be stored in or retrieved from a database. The SqlDateTime structure has a different underlying data structure from its corresponding .NET Framework type, DateTime, which can represent any time between 12:00:00 AM 1/1/0001 and 11:59:59 PM 12/31/9999, to the accuracy of 100 nanoseconds. SqlDateTime actually stores the relative difference to 00:00:00 AM 1/1/1900. Therefore, a conversion from "00:00:00 AM 1/1/1900" to an integer will return 0.

表示从1753年1月1日到9999年12月31日之间的日期和时间数据,该数据的精度为3.33毫秒,可以存储在数据库中或从数据库中检索。SqlDateTime结构的底层数据结构与它对应的。net框架类型DateTime不同,DateTime可以表示上午12:00:00 AM 1/0001到11:59:59 PM 12/31/9999之间的任何时间,精确度为100纳秒。SqlDateTime实际上将相对差异存储为00:00:00 AM 1/1/1900。因此,从“00:00 AM 1/1/1900”到整数的转换将返回0。

#2


3  

Subtract one from the other & check the ticks of the resulting TimeSpan to be within acceptable limits for the difference in tick length

从另一个中减去一个并检查结果的时间间隔是否在可接受的范围内

#3


0  

if you ignore millisecond difference than you can try this

如果你忽略毫秒差,你可以试试这个

Select * from MyTable DATEADD(ms, -DATEPART(ms, endTime), endTime) = @value

#1


2  

You can use the SqlDateTime structure.

您可以使用SqlDateTime结构。

DateTime now = DateTime.Now;
SqlDateTime sqlNow = new SqlDateTime(now);
bool equal = now == sqlNow.Value; // false

So if you have a DateTime and want to know if it's equal to a DB-DateTime use:

如果你有一个DateTime,想知道它是否等于DB-DateTime:

Assert.Equal(dbEndTime, new SqlDateTime(endTime).Value); //  true

SqlDateTime:

SqlDateTime:

Represents the date and time data ranging in value from January 1, 1753 to December 31, 9999 to an accuracy of 3.33 milliseconds to be stored in or retrieved from a database. The SqlDateTime structure has a different underlying data structure from its corresponding .NET Framework type, DateTime, which can represent any time between 12:00:00 AM 1/1/0001 and 11:59:59 PM 12/31/9999, to the accuracy of 100 nanoseconds. SqlDateTime actually stores the relative difference to 00:00:00 AM 1/1/1900. Therefore, a conversion from "00:00:00 AM 1/1/1900" to an integer will return 0.

表示从1753年1月1日到9999年12月31日之间的日期和时间数据,该数据的精度为3.33毫秒,可以存储在数据库中或从数据库中检索。SqlDateTime结构的底层数据结构与它对应的。net框架类型DateTime不同,DateTime可以表示上午12:00:00 AM 1/0001到11:59:59 PM 12/31/9999之间的任何时间,精确度为100纳秒。SqlDateTime实际上将相对差异存储为00:00:00 AM 1/1/1900。因此,从“00:00 AM 1/1/1900”到整数的转换将返回0。

#2


3  

Subtract one from the other & check the ticks of the resulting TimeSpan to be within acceptable limits for the difference in tick length

从另一个中减去一个并检查结果的时间间隔是否在可接受的范围内

#3


0  

if you ignore millisecond difference than you can try this

如果你忽略毫秒差,你可以试试这个

Select * from MyTable DATEADD(ms, -DATEPART(ms, endTime), endTime) = @value