I have a text field that will contain a given year, for example, “2011”. I need to calculate the value of the year 70 years earlier.
I have this code already, which supplies a default value for the text box:
var LastYear = DateTime.Now.AddYears(-1).ToString("yyyy"); //"2011"
Yeartextbox.Text = LastYear;
The user is allowed to change the value of the text box to whatever year they want. I need to take the data from the text box, and calculate 70 years earlier. For example, if the text box contains “2011” I need the result of 1941; if the user enters 2000 I need the result of 1930.
You are storing a year into the text box, not a date. Just, for example, 2011. That’s just a number, an integer, and you can do integer math on it. (The fact that it happens to be a year is irrelevant to the
-operator.)If you want to subtract 70 years from that, just do 2011 – 70: