比较两个日期时间值和表中的c#1,其他是当前值

时间:2021-05-18 14:17:30
da = new SqlDataAdapter("SELECT ExitTime,EnterTime,name,[tag-id-st],[build-id],[room-no],tagType FROM Students,GateLogging WHERE GateLogging.tagType='student'", MyConn);
DateTime ss=dt.Rows[0][1];       
DateTime date = DateTime.Now;
int x=date.CompareTo(ss);

This is my code, I stopped here and I can't complete

这是我的代码,我在这里停下来,我无法完成

Please help me to compare the values in "EnterTime" table with the current time and show in gridview the values that is more than the current time with 10 minutes

请帮我比较“EnterTime”表中的值和当前时间,并在gridview中显示超过10分钟当前时间的值

3 个解决方案

#1


First, find the date you actually care about - for example it might be

首先,找到您真正关心的日期 - 例如它可能是

DateTime cutoff = DateTime.Now.AddMinutes(10);

(do this once at the top, rather than per row) - then just compare against those, for example:

(在顶部执行此操作,而不是每行执行一次) - 然后只与这些进行比较,例如:

bool isOverdue = ss > cutoff;

You can also do this inside the database with GETDATE() and DATEADD(...) or DATEDIFF(...)

您也可以使用GETDATE()和DATEADD(...)或DATEDIFF(...)在数据库中执行此操作

#2


First of all you can change the select query to return only rows for a last 10 minutes, and you will not need to compare, Second you can use da.Fill(dt) to copy resultset to datatable and use dt.Select("...") to filter only your rows, and the third what is the problem? you can write

首先,您可以更改选择查询以仅返回最后10分钟的行,并且您不需要进行比较。其次,您可以使用da.Fill(dt)将结果集复制到datatable并使用dt.Select(“。 ..“)只过滤你的行,第三个是什么问题?你可以写

DateTime now = DateTime.Now;
if (now - ss <= TimeSpan.FromMinutes(10))
{

}

#3


Encountering the same problem in unit testing I wrote an AssertEx class with some less nitpicky assertions:

在单元测试中遇到同样的问题我写了一个AssertEx类,其中有一些不那么挑剔的断言:

public static void AreSimilar(DateTime expected, DateTime actual, TimeSpan tolerance, string message)
{
    DateTime expectedMin = expected.Subtract(tolerance);
    DateTime expectedMax = expected.Add(tolerance);

    if (actual < expectedMin || actual > expectedMax)
    {
        throw new AssertFailedException(message);
    }
}

#1


First, find the date you actually care about - for example it might be

首先,找到您真正关心的日期 - 例如它可能是

DateTime cutoff = DateTime.Now.AddMinutes(10);

(do this once at the top, rather than per row) - then just compare against those, for example:

(在顶部执行此操作,而不是每行执行一次) - 然后只与这些进行比较,例如:

bool isOverdue = ss > cutoff;

You can also do this inside the database with GETDATE() and DATEADD(...) or DATEDIFF(...)

您也可以使用GETDATE()和DATEADD(...)或DATEDIFF(...)在数据库中执行此操作

#2


First of all you can change the select query to return only rows for a last 10 minutes, and you will not need to compare, Second you can use da.Fill(dt) to copy resultset to datatable and use dt.Select("...") to filter only your rows, and the third what is the problem? you can write

首先,您可以更改选择查询以仅返回最后10分钟的行,并且您不需要进行比较。其次,您可以使用da.Fill(dt)将结果集复制到datatable并使用dt.Select(“。 ..“)只过滤你的行,第三个是什么问题?你可以写

DateTime now = DateTime.Now;
if (now - ss <= TimeSpan.FromMinutes(10))
{

}

#3


Encountering the same problem in unit testing I wrote an AssertEx class with some less nitpicky assertions:

在单元测试中遇到同样的问题我写了一个AssertEx类,其中有一些不那么挑剔的断言:

public static void AreSimilar(DateTime expected, DateTime actual, TimeSpan tolerance, string message)
{
    DateTime expectedMin = expected.Subtract(tolerance);
    DateTime expectedMax = expected.Add(tolerance);

    if (actual < expectedMin || actual > expectedMax)
    {
        throw new AssertFailedException(message);
    }
}