创建日期时间实例时的代码优化

本文关键字:代码优化 实例 时间 创建日期 | 更新日期: 2023-09-27 17:56:38

我 asp.net 网站上有一个名为ddlPayrollDate的下拉列表。我想插入/绑定过去 2 个月的日期 17 和 3,包括当月。为了考虑何时更改年份,我必须在 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)));

有没有办法通过使用任何日期时间函数来优化此代码,而无需具有这么多条件。提前感谢!

创建日期时间实例时的代码优化

在我看来

,你想要一个循环。这样:

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 日之后。

另请注意,在指定自定义日期格式时,通常应使用固定区域性;或者使用具有"正常"区域性的标准日期格式。

Use DateTime.AddMonth

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

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);
}