如何比较登录时间和注销时间。注销时间必须大于使用timepicker的登录时​​间

时间:2011-02-16 05:28:20

标签: c# .net timepicker

if (Convert.ToDateTime(GrdEmployeeAttendance.CurrentRow.Cells["LoginTime"].Value) < Convert.ToDateTime(GrdEmployeeAttendance.CurrentRow.Cells["LogoutTime"].Value))
{
    if (GrdEmployeeAttendance.Columns[e.ColumnIndex].Name == "LoginTime")
    {
        GrdEmployeeAttendance.EndEdit();

        if (Convert.ToDateTime(GrdEmployeeAttendance.CurrentRow.Cells[e.ColumnIndex].Value) < Convert.ToDateTime("01:00 PM"))
        {
            GrdEmployeeAttendance.CurrentRow.Cells["CheckFN"].Value = true;
        }
        else
        {
            GrdEmployeeAttendance.CurrentRow.Cells["ChkAN"].Value = true;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

要将登录时间与退出时间进行比较,您需要使用 DateTime.Compare method

它需要两个参数(要比较的DateTime个对象的两个实例),并返回一个整数,指示第一个是早于,等于或晚于第二个。

如果第一次更早,则返回值将小于0.如果两个时间值相同,则返回值将为0.如果第一次更晚,则返回值将大于零

示例代码:

DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;

if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";

Console.WriteLine("{0} {1} {2}", date1, relationship, date2);

// The example displays the following output:
//    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM