0

我的 asp.net 网站中有一个名为 ddlPayrollDate 的下拉列表。我想插入/绑定最近 3 个月的日期 2 和 17,包括当月。要考虑何时更改年份,我必须在我的 C# 代码中编写多个条件,下面给出的是我的代码:

     ddlPayrollDate.Items.Insert(1, String.Format("{0:MMMM dd, yyyy}", new DateTime (DateTime.UtcNow.Year, DateTime.UtcNow.Month, 17)));
                ddlPayrollDate.Items.Insert(2, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 2)));
                if (DateTime.UtcNow.Month != 1)
                    ddlPayrollDate.Items.Insert(3, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month - 1, 17)));
                else
                    ddlPayrollDate.Items.Insert(3, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 12, 17)));

                if (DateTime.UtcNow.Month != 1)
                    ddlPayrollDate.Items.Insert(4, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month - 1, 2)));
                else
                    ddlPayrollDate.Items.Insert(4, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 12, 2)));

                if (DateTime.UtcNow.Month != 1 && DateTime.UtcNow.Month != 2)
                    ddlPayrollDate.Items.Insert(5, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month - 2, 17)));
                else if (DateTime.UtcNow.Month == 1)
                    ddlPayrollDate.Items.Insert(5, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 11, 17)));
                else
                    ddlPayrollDate.Items.Insert(5, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 12, 17)));

                if (DateTime.UtcNow.Month != 1 && DateTime.UtcNow.Month != 2)
                    ddlPayrollDate.Items.Insert(6, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month - 2, 2)));
                else if (DateTime.UtcNow.Month == 1)
                    ddlPayrollDate.Items.Insert(6, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 11, 2)));
                else
                    ddlPayrollDate.Items.Insert(6, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 12, 2)));

有没有一种方法可以通过使用任何日期时间函数来优化此代码而无需满足这些条件。提前致谢!

4

3 回答 3

4

在我看来,你想要一个循环。

DateTime now = DateTime.UtcNow;
// Use the start of the month to avoid concerns around short months etc
DateTime current = new DateTime(now.Year, now.Month, 1);

for (int i = 0; i < 3; i++)
{        
    // Add the 17th... (add 16 days to the 1st)
    ddlPayrollDate.Items.Insert(i * 2 + 1,
                                current.AddDays(16).ToString("MMMM dd, yyyy"));
    // Add the 2nd... (add 1 day to the 1st)
    ddlPayrollDate.Items.Insert(i * 2 + 2,
                                current.AddDays(1).ToString("MMMM dd, yyyy"));
    // Go back a month...
    current = current.AddMonths(-1);
}

这假设您总是想要当月的第 17 日和第 2(以 UTC 为单位),无论当前日期是在 2 日之前、2 日和 17 日之间还是 17 日之后。

另请注意,在指定自定义日期格式时,您通常应该使用不变文化;或使用具有“正常”文化的标准日期格式。

于 2013-01-30T18:39:17.357 回答
0

使用DateTime.AddMonths

例如,DateTime.UtcNow.AddMonths(-1) 获取今天日期前 1 个月的日期。

于 2013-01-30T18:36:18.223 回答
0
DateTime startDate = DateTime.Today.AddMonths(-3);

for (Int32 indexer=0; indexer < 3; indexer++) {
  ddlPayrollDate.Items.Add(String.Format("{0:MMMM dd, yyyy}", new DateTime(startDate,Year, startDate.Month, 2)));
ddlPayrollDate.Items.Add(String.Format("{0:MMMM dd, yyyy}", new DateTime(startDate,Year, startDate.Month, 17)));
startDate = startDate.AddMonth(1);
}
于 2013-01-30T18:37:55.677 回答