首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算时间( asp.net )

问计算时间( asp.net )
EN

Stack Overflow用户
提问于 2015-04-15 22:31:51
回答 1查看 228关注 0票数 0

我使用sql server存储员工进进出出的午餐时间,并使用asp.net计算差额,如下所示。现在我想看看如果它们超过30分钟,那么值应该是红色的。

代码语言:javascript
复制
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));
}

我该怎么做呢?谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-04-15 22:41:51

一般来说,我更喜欢使用真实的数据源,而不是GridViewRows中的文本。您可以通过e.Row.DataItem在RowDataBound中轻松获取。这样就可以防止本地化问题和对原始数据的处理。

但是,要从两个DateTime中获得以分钟为单位的时间,您可以将它们相减,然后使用返回的TimeSpan的TotalMinutes属性:

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29653067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档