public ActionResult Create(ReportingPeriod reportingPeriod) { //1 для первого дня, т.к есть только год и месяц var currentDate = new DateTime(reportingPeriod.Year, reportingPeriod.Month, 1); var firstDayOnNextMonth = new DateTime(currentDate.Year, currentDate.AddMonths(1).Month, 1); DateTime firstDayPlusTwoMonths = firstDayOnNextMonth.AddMonths(2); DateTime lastDayNextMonth = firstDayPlusTwoMonths.AddDays(-1); DateTime endOfLastDayNextMonth = firstDayPlusTwoMonths.AddTicks(-1); if (reportingPeriod.NewTSRecordsAllowedUntilDate == new DateTime()) { //Если указан декабрь if (currentDate.Month == 12) { reportingPeriod.NewTSRecordsAllowedUntilDate = new DateTime(currentDate.AddYears(1).Year, currentDate.AddMonths(1).Month, 3); } else { reportingPeriod.NewTSRecordsAllowedUntilDate = new DateTime(currentDate.Year, currentDate.AddMonths(1).Month, 3); } } else { var setValue = reportingPeriod.NewTSRecordsAllowedUntilDate; //Если указан не тот год или отчитаться на следующий год //if (DateTime.Now.Year != setValue.Year || currentDate.AddMonths(1).Year != DateTime.Now.Year) // ModelState.AddModelError("NewTSRecordsAllowedUntilDate", "Вы не можете указать дату окончания в декабре на январь или указан не тот год"); if (setValue < firstDayOnNextMonth) { ModelState.AddModelError("NewTSRecordsAllowedUntilDate", "Вам надо указать дату включительно " + "от первого числа следующего месяца"); } } if (reportingPeriod.TSRecordsEditApproveAllowedUntilDate <= firstDayOnNextMonth) { ModelState.AddModelError("TSRecordsEditApproveAllowedUntilDate", "Дата полного закрытия должна быть не раньше первого числа следующего месяца."); } if (reportingPeriod.TSRecordsEditApproveAllowedUntilDate <= reportingPeriod.NewTSRecordsAllowedUntilDate) { ModelState.AddModelError("TSRecordsEditApproveAllowedUntilDate", "Дата полного закрытия не должна быть меньше или равна дате закрытия месяца."); } ModelState.Clear(); if (ModelState.IsValid) { _reportingPeriodService.Add(reportingPeriod); return(RedirectToAction("Index")); } SetViewBag(reportingPeriod); return(View(reportingPeriod)); }