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;
}
}
}
To compare the login time to the logout time, you will want to use the DateTime.Compare
method.
It takes two arguments (the two instances of a DateTime
object to be compared), and returns an integer that indicates whether the first is earlier than, the same as, or later than the second.
If the first time is earlier, the return value will be less than 0. If the two time values are the same, the return value will be 0. If the first time is later, the return value will be greater than zero.
Sample code:
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
精彩评论