Subtract days from the DateTime format
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
Related videos on Youtube
Batuhan
Updated on June 04, 2022Comments
-
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 almost 7 yearsWhat do you want to check in your if condition?
-
Tetsuya Yamamoto almost 7 years
if (followingTrialDate == followingTrialDate.AddDays(-14))
, is this enough? As general rule, if condition must usebool
or expression which returnsbool
. -
ADyson almost 7 yearsan
if
must evaluate something to true or false in order to decide whether to go into the if block or not. The result ofDateTime.addDays
is anotherDateTime
object. You can't interpret aDateTime
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 almost 7 yearsYour if condition does not make sense even in natural language: if following trial date minus 14 days... What?
-
Batuhan almost 7 yearsI 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 over 6 yearsPossible duplicate of How to subtract a day from a DateTime?
-