C# DateTime AddMonths 的错误用法导致跳过日期
class Program
{
static void Main(string[] args)
{
DateTime now = DateTime.Now;
for (int i = 0; i < 60; ++i)
{
DateTime dt2 = now.AddMonths(+i);
//Console.WriteLine(dt2.ToString("yyyyMM"));
DateTime dt3 = now.AddDays(+i);
Console.WriteLine(dt3.ToString("yyyyMMdd"));
}
Console.Read();
}
// AddMonths的错误用法
private void error()
{
DateTime now = DateTime.Now;
for (int i = 0; i < 20; ++i)
{
// 这种在2020-12-31执行时会退跳过202102
now = now.AddMonths(+i);
Console.WriteLine(now.ToString("yyyyMM"));
}
Console.Read();
}
}