Subtract days from the DateTime format

11,078

Solution 1

You're subtracting the days fine. followingTrialDate.AddDays(-14) is correct, it returns the followingTrialDate minus the 14 days (as a new DateTime). The issue is you've not got any comparison in the if statement:

Can not implicitly Convert Type 'System.DateTime' to 'bool'

Describes the problem exactly. Your code is similar to:

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

Which produces the exact same error. If the date time what? It cannot be true or false. If only operates on a boolean.

I suspect you want to compare it to some other date time with > or <.

This would work, for example:

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

Solution 2

you have an error in if statement. Function AddDays returns DateTime (link) and not bool.

If you want to subtract days, you are on the correct path:

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

Solution 3

Cannot implicitly Convert Type 'System.DateTime' to 'bool'

The DateTime.AddDays method Returns a new DateTime that adds the specified number of days to the value of this instance. Whereas if expect a condition that evaluates to a boolean value. this mismatch leads to the above specified error.

how to subtract from DateTime type 14 days ?

For this you have to assign the result of AddDays() to a DateTime variable like the following:

DateTime PrevDate = followingTrialDate.AddDays(-14);
// Proceed with PrevDate 
Share:
11,078

Related videos on Youtube

Batuhan
Author by

Batuhan

Updated on June 04, 2022

Comments

  • Batuhan
    Batuhan almost 2 years
    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,";
    }
    

    It gives an error in the "if" cycle. Error:

    Cannot implicitly Convert Type 'System.DateTime' to 'bool'

    I know the error, the "if" loop gets bool value. But I could not get a date from the DateTime type. How to subtract from DateTime type 14 days ?

    • Mighty Badaboom
      Mighty Badaboom almost 7 years
      What do you want to check in your if condition?
    • Tetsuya Yamamoto
      Tetsuya Yamamoto almost 7 years
      if (followingTrialDate == followingTrialDate.AddDays(-14)), is this enough? As general rule, if condition must use bool or expression which returns bool.
    • ADyson
      ADyson almost 7 years
      an if must evaluate something to true or false in order to decide whether to go into the if block or not. The result of DateTime.addDays is another DateTime object. You can't interpret a DateTime as being true or false. Your if statement does not make any sense. In English your if statement reads as "If trialDate minus 14 days". It's incomplete and nonsensical. Did you mean to compare it to some other date, maybe the current date?
    • Federico Dipuma
      Federico Dipuma almost 7 years
      Your if condition does not make sense even in natural language: if following trial date minus 14 days... What?
    • Batuhan
      Batuhan almost 7 years
      I mean, 19.07.2017 - 14 Days = 05.07.2017 should be this way. How can I write this in the "if" loop?
    • Sayed Abolfazl Fatemi
      Sayed Abolfazl Fatemi over 6 years