我仍在学习 C#,我的 SAP 附加薪资程序有问题。
我有一个表单,用户可以在其中选择开始时间。即“2011 年 1 月 1 日”通过 Visual Basic 2010 创建的日期时间选择器 (1)。
我希望表单上的另一个日期时间选择器 (2) 在整整一年后自动显示结束时间。在开始期间 =“2011 年 1 月 1 日”的情况下,我希望结束期间为“2011 年 12 月 31 日”。
我的代码:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
var endDate = default(DateTime);
endDate = dateTimePicker1.Value.AddDays(365);
dateTimePicker2.Value = endDate;
}
我的两个问题。
以上不考虑闰年。我该如何解决这个问题?
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与第一个问题有关吗?
感谢任何帮助。
最佳答案
dateTimePicker1.Value.AddYears(1).AddDays(-1);
关于c# - 使结束日期值在用户输入开始日期并考虑闰年后准确显示 1 年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6530957/