C# 在asp.net中计算时间
我使用sql server存储员工午餐的进出时间,并使用asp.net计算差异,如下所示。现在我想看看它们是否超过30分钟,然后值应该是红色C# 在asp.net中计算时间,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我使用sql server存储员工午餐的进出时间,并使用asp.net计算差异,如下所示。现在我想看看它们是否超过30分钟,然后值应该是红色 if (e.Row.RowType == DataControlRowType.DataRow) { //Lunch Total Time Label lunIn = (Label)e.Row.FindControl("lblLunIn"); Label lunOut = (Label)e.Row.FindControl("lblL
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Lunch Total Time
Label lunIn = (Label)e.Row.FindControl("lblLunIn");
Label lunOut = (Label)e.Row.FindControl("lblLunOut");
Label lunTotal = (Label)e.Row.FindControl("lblLunTotal");
lunTotal.Text = Convert.ToString(Convert.ToDateTime(lunOut.Text) - Convert.ToDateTime(lunIn.Text));
}
我该怎么做呢?谢谢总的来说,我更喜欢使用真实的数据源,而不是
GridViewRows
中的文本。您可以通过e.Row.DataItem
在RowDataBound
中轻松获取它。通过这种方式,您可以防止本地化问题和使用原始数据的工作
但是,要从两个DateTime
s中获取以分钟为单位的时间,可以将它们相减,然后使用返回的TimeSpan
sTotalMinutes
属性:
DateTime dtIn = Convert.ToDateTime(lunIn.Text);
DateTime dtOut = Convert.ToDateTime(lunOut.Text);
TimeSpan duration = dtOut - dtin;
lunTotal.Text = duration.ToString();
if(duration.TotalMinutes > 30)
{
e.Row.BackColor = Color.Red;
// or maybe you just want to change the color of the Label
lunTotal.ForeColor = Color.Red;
}
你在哪个国家工作?我很感兴趣,因为那听起来更像中国或朝鲜。然而,你不知道如何计算总时间的问题是什么?使用数据源而不是GridViewRows。查看
System.TimeSpan
类。我得到了总时间,但想从这里知道。。如果“lunTotal”大于30分钟,我如何将其设置为红色。