There are two text fields and date verification with the condition if more than a year.

The first current date and the second end date (we fill in the field ourselves).
Need verification if more than 12 months then an error

Code:

DateTime dateBegin = DateTime.Parse(txtDateBegin.Text); DateTime dateEnd = DateTime.Parse(txtDateEnd.Text); TimeSpan sp = dateEnd - dateBegin; if (DateTime.IsLeapYear(dateEnd.Year)) { if (sp.Days > 366) { args.IsValid = false; CV_InsDates.ErrorMessage = "Ошибка"; } } else { if (sp.Days > 365) { args.IsValid = false; CV_InsDates.ErrorMessage = "Ошибка"; } } 

Will this check be correct (check only on the end date)?
With what number to compare 366 or 365, how to determine how many days in a year?

  • No, you can check on 02.27-27-27.2017 ideone.com/CaRtFr - Zufir
  • I understand it is necessary that the difference between dates was not more than one year? - Vadim Ovchinnikov
  • @Vadim Ovchinnikov, yes, the question was, on what date should I put IsLeaoYear () on the end date? - Zhandos
  • one
    @Zhandos highly recommend changing your question so that it is useful to society. 1. Why is asp.net here? You can remake the question about dates (let's say on the method that returns bool and remove the specifics of asp.net ). 2. Indicate about your desire for the code to return the bool : whether the difference is less than 1 year. Then your question will be more general and useful. - Vadim Ovchinnikov
  • Check leap year or not (in a given period) , it's like? If less than one year intervals? - Vadim Ovchinnikov

3 answers 3

It is not quite clear where such a strange condition comes from. But if it really is - why bother to separate years for leap years and not, if you can just compare two dates?

 if (dateBegin < dateEnd.AddYears(-1)) { // Ошибка } 

Here are 4 options of conditions that can be used depending on the task (what is considered a year):

  • dateBegin < dateEnd.AddYears(-1) - allows (Feb. 28 ... Feb. 29);
  • dateBegin.AddYears(1) < dateEnd - prohibits (Feb. 28 ... Feb. 29), but allows (Feb. 28 ... Feb. 28);
  • dateBegin <= dateEnd.AddYears(-1) - prohibits (Feb. 28 ... Feb. 28), but allows (Feb. 29 ... Feb. 28);
  • dateBegin.AddYears(1) <= dateEnd - prohibits (Feb 29 ... Feb 28).
  • compared two dates if (dateEnd.AddMonths (-12) .AddDays (1)> dateBegin) - Zhandos
  • one
    @Zhandos and why AddYears bad? Why .AddMonths(-12) something? - Pavel Mayorov
  • one
    @Zhandos, by the way, your condition is equivalent to my third option - Pavel Mayorov

I would try hard to avoid the difference in extraction over the years because of the leap year because of the "bikes" and the corresponding bugs. For example, you did not take into account the cases

  1. When in the first date a leap year and a date earlier than February 29.
  2. When the second date is a leap year and the date is earlier February 29th.

To find out the difference in years, you need a code (taken from English SO )

 DateTime zeroTime = new DateTime(1, 1, 1); TimeSpan span = startDate - endDate; // Because we start at year 1 for the Gregorian // calendar, we must subtract a year here. int years = (zeroTime + span).Year - 1; 

Then check

 bool isValid = years < 1; 

    How to determine the number of days in the period?

    There is a DateAndTime.DateDiff method that returns the number of time intervals between two Date values.

    • It would be nice to expand the answer a bit so that it would not be deleted as a "link to the answer instead of the answer" - Kromster
    • For God's sake. Only it will be a disservice to the author, if he does not have time to see the link, and no benefit to the rest. - Akina
    • And in C #, does this class really exist? - Zufir
    • MSDN thinks there is. Would you like to argue with her? - Akina
    • No, I do not want :) I forgot to connect Microsoft.VisualBasic. I wonder why this method was not duplicated in the standard DateTime - Zufir