C# 使用条件计算日期时间持续时间

C# 使用条件计算日期时间持续时间,c#,asp.net,C#,Asp.net,我想用条件计算DateTime持续时间:如果“STATUSIN”>08:00:00,那么“LATECOME”=“STATUSIN”-08.00.00 我一直在尝试这个代码,但它不起作用 DataTable dtDaily = new DataTable(); string time1 = "08:00:00"; var result = Convert.ToDateTime(time1); string test = result.ToString("hh:mm:ss"); foreach (

我想用条件计算DateTime持续时间:
如果“STATUSIN”>08:00:00,那么“LATECOME”=“STATUSIN”-08.00.00
我一直在尝试这个代码,但它不起作用

DataTable dtDaily = new DataTable();
string time1 = "08:00:00";
var result = Convert.ToDateTime(time1);
string test = result.ToString("hh:mm:ss");

foreach (DataRow pRow in dtDaily.Rows)
{
   DateTime.ParseExact(pRow["STATUSIN"].ToString(), "hh:mm:ss", CultureInfo.InvariantCulture);
   if (pRow["STATUSIN"] > time1)
   {
      pRow["LATECOME"] = pRow["STATUSIN"] - time1.ToString();
   }
}
这是我在表中显示值的代码:

<asp:Repeater ID="rptrSUMMARYDATA" runat="server">
    <HeaderTemplate>
        <table class="table">
            <thead>
                <tr>
                    <th>IN</th>
                    <th>LATECOME</th>
                </tr>
            </thead>
            <tbody>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><%# Eval("STATUSIN") %></td>
            <td><%# Eval("LATECOME") %></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
            </tbody>
        </table>
    </FooterTemplate>
</asp:Repeater> 

在里面
晚来
更改代码

 string time1 = "08:00:00";
DateTime.ParseExact(pRow["STATUSIN"].ToString(), "hh:mm:ss", CultureInfo.InvariantCulture);
if (pRow["STATUSIN"] > time1)
{
    pRow["LATECOME"] = pRow["STATUSIN"] - time1.ToString();
}
替换代码

DateTime time1 = DateTime.Parse("08:00:00");
 Datetime d=DateTime.ParseExact(pRow["STATUSIN"].ToString(), "hh:mm:ss", CultureInfo.InvariantCulture);
   if (d > time1)
   {
      pRow["LATECOME"] =d - time1.ToString();
   }

代码无法工作的原因是字符串数据的比较。您可以检查字符串是否大于彼此,但不能检查您的思维方式

如果要比较这两个日期,必须首先将字符串转换为DateTime,然后检查哪个日期早于或晚于另一个日期

DataTable dtDaily = new DataTable();

// If you want to compare the Time, use TimeSpan
TimeSpan time1 = new TimeSpan(8, 0, 0);

foreach (DataRow pRow in dtDaily.Rows)
{
   if (pRow.Field<DateTime>("STATUSIN").TimeOfDay > time1)
   {
      // This will ONLY be the time portion.. not the full datetime. Notice the 
      // parenthesis around the the statement on the right. You want to convert the resulting
      // Time (after subtraction) to string.
      pRow["LATECOME"] = (pRow.Field<DateTime>("STATUSIN").TimeOfDay  - time1).ToString();
   }
}
DataTable dtDaily=newdatatable();
//如果要比较时间,请使用TimeSpan
TimeSpan time1=新的TimeSpan(8,0,0);
foreach(dtDaily.Rows中的数据行前端)
{
if(船首字段(“状态输入”).TimeOfDay>time1)
{
//这将只是时间部分,而不是完整的日期时间。请注意
//右侧语句周围的括号。是否要转换结果
//字符串的时间(减法后)。
船头[“LATECOME”]=(船头字段(“STATUSIN”).TimeOfDay-time1.ToString();
}
}
pRow.Field(“Field”)
获取数据的字符串表示形式并将其转换为DateTime。这类似于
DateTime.Parse(pRow[“Field”])
,但取消了进行转换的步骤


在时间跨度上

公共时间跨度(整数小时、整数分钟、整数秒);


谢谢,我一直在尝试该代码,但仍然不起作用。错误是:“运算符不能应用于'DateTime'和'String'类型的操作数”“。如何解决呢?@deananda您可以使用time1也可以定义类型为datetime但是,为什么该值仍然为null?鉴于我在
STATUSIN
08:06:00不返回null中有一个日期值,请尝试此操作,我想运行代码并返回00:06.00。在这个问题上,您能帮我吗?我认为这与这个问题类似