我的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)));
有没有一种方法可以通过使用任何日期时间函数来优化此代码,而无需具备这些条件。提前谢谢!
答案 0 :(得分:4)
在我看来,你想要一个循环。 Something 喜欢:
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日。
另请注意,在指定自定义日期格式时,通常应使用不变文化;或使用具有“正常”文化的标准日期格式。
答案 1 :(得分:0)
例如,DateTime.UtcNow.AddMonths(-1)获取今天日期前1个月的日期。
答案 2 :(得分: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);
}