我知道c#有一个方法可以求出一年中的第n天
//
// 摘要:
// 获取此实例所表示的日期是该年中的第几天。
//
// 返回结果:
// 该年中的第几天,表示为 1 和 366 之间的一个值。
public int DayOfYear { get; }
现在我想输入一个一年中的第n天,计算出它是几月几日
从元数据看不懂,以下是我的笨方法
请问有没有更简便的方法?
public void ReDOY(int d)
{
if (radioButton1.Checked)
{
if (d >= 1 && d <= 31) dateTimePicker1.Value = new DateTime(2019, 1, d -= 0);
else if (d >= 32 && d <= 59) dateTimePicker1.Value = new DateTime(2019, 2, d -= 31);
else if (d >= 60 && d <= 90) dateTimePicker1.Value = new DateTime(2019, 3, d -= 59);
else if (d >= 91 && d <= 120) dateTimePicker1.Value = new DateTime(2019, 4, d -= 90);
else if (d >= 121 && d <= 151) dateTimePicker1.Value = new DateTime(2019, 5, d -= 120);
else if (d >= 152 && d <= 181) dateTimePicker1.Value = new DateTime(2019, 6, d -= 151);
else if (d >= 182 && d <= 212) dateTimePicker1.Value = new DateTime(2019, 7, d -= 181);
else if (d >= 213 && d <= 243) dateTimePicker1.Value = new DateTime(2019, 8, d -= 212);
else if (d >= 244 && d <= 273) dateTimePicker1.Value = new DateTime(2019, 9, d -= 243);
else if (d >= 274 && d <= 304) dateTimePicker1.Value = new DateTime(2019, 10, d -= 273);
else if (d >= 305 && d <= 334) dateTimePicker1.Value = new DateTime(2019, 11, d -= 304);
else if (d >= 335 && d <= 365) dateTimePicker1.Value = new DateTime(2019, 12, d -= 334);
else MessageBox.Show("平年为365天,请检查你的输入");
}
else if (radioButton2.Checked)
{
if (d >= 1 && d <= 31) dateTimePicker1.Value = new DateTime(2020, 1, d -= 0);
else if (d >= 32 && d <= 60) dateTimePicker1.Value = new DateTime(2020, 2, d -= 31);
else if (d >= 61 && d <= 91) dateTimePicker1.Value = new DateTime(2020, 3, d -= 60);
else if (d >= 92 && d <= 121) dateTimePicker1.Value = new DateTime(2020, 4, d -= 91);
else if (d >= 122 && d <= 152) dateTimePicker1.Value = new DateTime(2020, 5, d -= 121);
else if (d >= 153 && d <= 182) dateTimePicker1.Value = new DateTime(2020, 6, d -= 152);
else if (d >= 183 && d <= 213) dateTimePicker1.Value = new DateTime(2020, 7, d -= 182);
else if (d >= 214 && d <= 244) dateTimePicker1.Value = new DateTime(2020, 8, d -= 213);
else if (d >= 245 && d <= 274) dateTimePicker1.Value = new DateTime(2020, 9, d -= 244);
else if (d >= 275 && d <= 305) dateTimePicker1.Value = new DateTime(2020, 10, d -= 274);
else if (d >= 306 && d <= 335) dateTimePicker1.Value = new DateTime(2020, 11, d -= 305);
else if (d >= 336 && d <= 366) dateTimePicker1.Value = new DateTime(2020, 12, d -= 335);
else MessageBox.Show("闰年为366天,请检查你的输入");
}
}
public void Calc(object sender, EventArgs e)
{
try{ ReDOY((int)numericUpDown1.Value); }
catch (Exception err) { MessageBox.Show(err.Message); }
}