我今天才开始收到这个异常,2/1/21。下面有一个解决方案,但仍然不知道为什么最初的代码行停止工作。
// Throws Exception
// System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.
DateTime fromDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.AddDays(-1).Day, 0, 0, 0);
// This works
DateTime start = DateTime.Now.AddDays(-1);
DateTime startDateTime = new DateTime(start.Year, start.Month, start.Day, 0, 0, 0);发布于 2021-02-01 21:25:28
因为DateTime.Now.AddDays(-1).Day给出了31天,而2月只有28天。
发布于 2021-02-01 21:29:28
您可以将代码简化为
DateTime startDateTime = DateTime.Today.AddDays(-1);并避免完全遇到这个问题。
https://stackoverflow.com/questions/66000253
复制相似问题