【问题标题】:Subtract days from the DateTime format从 DateTime 格式中减去天数
【发布时间】:2017-07-19 09:34:09
【问题描述】:
DateTime followingTrialDate = Convert.ToDateTime(row["FollowingTrialDate"]);
if (followingTrialDate.AddDays(-14))
{
  ModuleBody = davaTarih.ToShortDateString() + " tarihli dava'ya 14 (on dört) gün kalmıştır.";
  ModuleBody = "sayın ilgili, \r\n \r\n" + "esas no: " + esasNo + "\r\n" + "dava tarihi: " + davaTarih.ToShortDateString() + "\r\n" + "sonraki duruşma tarihi: " + sonrakidurusmaTar + "\r\n" + "kısa açıklama: " + kısaAciklama + "\r\n\r\n" + "bilginize,";
}

它在“if”循环中给出错误。错误:

无法将类型“System.DateTime”隐式转换为“bool”

我知道错误,“if”循环获取布尔值。但我无法得到约会 日期时间类型。如何从 DateTime 类型中减去 14 天?

【问题讨论】:

  • 你想在你的 if 条件中检查什么?
  • if (followingTrialDate == followingTrialDate.AddDays(-14)),够了吗?作为一般规则,if 条件必须使用bool 或返回bool 的表达式。
  • an if 必须将某事评估为真或假,才能决定是否进入 if 块。 DateTime.addDays 的结果是另一个 DateTime 对象。您不能将DateTime 解释为真或假。您的 if 语句没有任何意义。在英语中,您的 if 语句读作“如果 trialDate 减去 14 天”。这是不完整和荒谬的。您的意思是要将它与其他日期(可能是当前日期)进行比较吗?
  • 您的 if 条件即使在自然语言中也没有意义:如果以下试用日期减去 14 天... 什么?
  • 我的意思是,19.07.2017 - 14 Days = 05.07.2017 应该是这样的。我怎样才能在“if”循环中写这个?

标签: c# datetime timespan


【解决方案1】:

您正在减去天数。 followingTrialDate.AddDays(-14) 是正确的,它返回 followingTrialDate 减去 14 天(作为新的 DateTime)。问题是您在 if 语句中没有任何比较:

不能将类型“System.DateTime”隐式转换为“bool”

准确描述问题。您的代码类似于:

if (new DateTime()) {
    // do something
}

这会产生完全相同的错误。如果日期时间是什么?不能是truefalseIf 仅对布尔值进行操作。

我怀疑您想将其与 >< 的其他日期时间进行比较。

这会起作用,例如:

if (followingTrialDate.AddDays(-14) < DateTime.Now)

【讨论】:

    【解决方案2】:

    你在 if 语句中有一个错误。函数 AddDays 返回 DateTime (link) 和 not bool。

    如果你想减去天数,你就走在正确的道路上:

    DateTime dt = new DateTime(2017, 7, 20);
    dt = dt.AddDays(-5);
    Console.WriteLine(dt.ToString()); // output: 15. 07. 2017
    

    【讨论】:

      【解决方案3】:

      无法将类型“System.DateTime”隐式转换为“bool”

      DateTime.AddDays 方法返回一个新的 DateTime,它将指定的天数添加到此实例的值中。而if 期望一个计算为布尔值的条件。这种不匹配会导致上述指定错误。

      如何从 DateTime 类型中减去 14 天?

      为此,您必须将 AddDays() 的结果分配给 DateTime 变量,如下所示:

      DateTime PrevDate = followingTrialDate.AddDays(-14);
      // Proceed with PrevDate 
      

      【讨论】:

        【解决方案4】:
        DateTime followingTrialDate = Convert.ToDateTime(row["FollowingTrialDate"]).AddDays(-14);
        

        会成功的。不过,您可能希望在此处进行一些错误处理(如果 row["FollowingTrialDate"] 无法转换为 DateTime 并因此引发异常怎么办?)。

        剩下的唯一问题是你想在if-condition 中检查什么。

        【讨论】:

          【解决方案5】:

          if 语句必须是布尔表达式 - 日期不是布尔表达式

          相反,当您从 DataRow 获取日期并且它可能为空时,为什么不尝试这样的方法,它将检查该行是否为空,如果不是,则计算日期前 14 天。

          if (row["FollowingTrialDate"] != null)
          {
              DateTime followingTrialDate = Convert.ToDateTime(row["FollowingTrialDate"]);
              followingTrialDate = followingTrialDate.AddDays(-14);
              ModuleBody = davaTarih.ToShortDateString() + " tarihli dava'ya 14 (on dört) gün kalmıştır.";
              ModuleBody = "sayın ilgili, \r\n \r\n" + "esas no: " + esasNo + "\r\n" + "dava tarihi: " + davaTarih.ToShortDateString() + "\r\n" + "sonraki duruşma tarihi: " + sonrakidurusmaTar + "\r\n" + "kısa açıklama: " + kısaAciklama + "\r\n\r\n" + "bilginize,";
          }
          

          【讨论】:

            【解决方案6】:

            这里我们还有另一种从 DateTime 中减去天数的方法

            DateTime oldDt = dt.Subtract(TimeSpan.FromDays(14));
            

            row["FollowingTrialDate"] 应解析为DateTime 以进行比较。 例如,格式为mm/dd/yyyy

            你可以这样处理:

            if(row["FollowingTrialDate"]!=null
            {
               DateTime dt=DateTime.ParseExact(row["FollowingTrialDate"].ToString(),"mm/dd/yyyy");
               if( dt < oldDt ) //or any date comparison you like
               {
                  //code here
               }
            }
            

            【讨论】:

              猜你喜欢
              • 2012-06-24
              • 1970-01-01
              • 2011-05-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-07-07
              • 1970-01-01
              相关资源
              最近更新 更多