Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net中计算时间_C#_Asp.net_Sql Server - Fatal编程技术网

C# 在asp.net中计算时间

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

我使用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("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
s
TotalMinutes
属性:

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分钟,我如何将其设置为红色。