代码之家  ›  专栏  ›  技术社区  ›  Batuhan

从日期时间格式中减去天数

  •  2
  • Batuhan  · 技术社区  · 9 年前
    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”

    日期时间类型。如何从日期时间类型中减去14天?

    6 回复  |  直到 9 年前
        1
  •  4
  •   Joe    9 年前

    followingTrialDate.AddDays(-14) 是正确的,它返回 followingTrialDate DateTime

    无法隐式转换类型“System”。DateTime“到bool”

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

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

    true 或 false . If

    我怀疑你想把它与其他约会时间进行比较 > 或 < .

    这将起作用,例如:

    if (followingTrialDate.AddDays(-14) < DateTime.Now)
    
        2
  •  3
  •   Ales    9 年前

    link )和 不

    如果要减去天数,您的路径是正确的:

    DateTime dt = new DateTime(2017, 7, 20);
    dt = dt.AddDays(-5);
    Console.WriteLine(dt.ToString()); // output: 15. 07. 2017
    
        3
  •  1
  •   sujith karivelil    9 年前

    无法隐式转换类型“System”。DateTime“到bool”

    DateTime.AddDays 方法返回一个新的日期时间,该日期时间将指定的天数添加到此实例的值中。鉴于 if 预期条件的计算结果为布尔值。这种不匹配会导致上述指定错误。

    如何从日期时间类型中减去14天?

    AddDays() 到DateTime变量,如下所示:

    DateTime PrevDate = followingTrialDate.AddDays(-14);
    // Proceed with PrevDate 
    
        4
  •  0
  •   waka    9 年前
    DateTime followingTrialDate = Convert.ToDateTime(row["FollowingTrialDate"]).AddDays(-14);
    

    会成功的。不过,您可能需要在这里进行一些错误处理(如果 row["FollowingTrialDate"]

    if -条件。

        5
  •  0
  •   Mauro    9 年前

    一 if

    相反,当您从数据行获取日期时,它可能为空,为什么不尝试这样的操作,它将检查该行是否为空,如果不是,则计算日期前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
  •  0
  •   Stephen Rauch Afsar Ali    8 年前

    这里我们有另一种从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
       }
    }