如何在C#中迭代一年中的一系列天?

时间:2014-10-02 11:28:53

标签: c# arrays loops datetime

吨。学生和我有这个挑战来创建一个程序(在C#中)来计算一年中过去的天数,而不使用DayOfYear属性,我正在使用带有日期时间选择器的Windows窗体应用程序来选择日期计算,这是我到目前为止,它不起作用,任何建议?

    private void dtmSelectedDate_ValueChanged(object sender, EventArgs e)
    {
        DaysSoFarThisYear();
    }

    private void DaysSoFarThisYear()
    {
        int daysInMonth = dtmSelectedDate.Value.Day;
        int monthInYear = dtmSelectedDate.Value.Month;
        int daysSoFar = 0;

        int[,] daysOfYear = new int[daysInMonth, monthInYear];

        for (int row = 0; row < daysOfYear.GetLength(0); row++)
        {
            for (int col = 0; col < daysOfYear.GetLength(1); col++)
            {
                daysSoFar++;
            }
        }

        txtCalculated.Text = Convert.ToString(daysSoFar);

       // DateTime dayOfYear = new DateTime(dtmSelectedDate.Value.Year, dtmSelectedDate.Value.Month, dtmSelectedDate.Value.Day);
       // txtFromNET.Text = Convert.ToString(dayOfYear.DayOfYear);
    }
}

1 个答案:

答案 0 :(得分:0)

你可以试试这个

TimeSpan time= DateTime.Now.Subtract(new DateTime(DateTime.Now.Year, 01, 01));
int numberOfDays = time.Days;