Im still learning C# and I have a problem with my SAP add-on Payroll program.
I have a form where a user selects a starting period. I.e “01 January 2011” through a datetime picker (1) created through visual basic 2010.
I want another datetime picker (2) on the form to automatically show the ending period after exatcly one year. In the case of starting period = “01 January 2011”, I want the ending period to be “31st December 2011”.
My Code:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
var endDate = default(DateTime);
endDate = dateTimePicker1.Value.AddDays(365);
dateTimePicker2.Value = endDate;
}
My 2 problems.
-
The above does not account for leap years. How do I solve this?
-
With the above code, when 01 January 2011 is selected, the ending date is showing incorrectly, 01 January 2012, however when I input 01 march 2011, the ending date is showing correctly, 29 February 2012. Could this be related to the first problem?
Any help appreciated.
1 Answer