Check valid date

So, I'm trying to figure out if there is another way to check if the date is valid. Thus, the idea is that if the date is valid, then it continues to use this date, if the date is invalid, it uses the date of today.

This is what I got at the moment:

public void setBirthdate(int year, int month, int day) { if (month < 1 || month > 12 || day < 1 || day > DateTime.DaysInMonth(year, month)) { Birthdate = DateTime.Today; } else Birthdate = new DateTime(year, month, day); } 

So, is there a shorter / more readable way to do this?

Thank you in advance

+7
source share
5 answers

You can use the values ​​to try to build a valid DateTime , and then catch the ArgumentOutOfRangeException , which occurs if the arguments are out of range:

 public void setBirthdate(int year, int month, int day) { try { Birthdate = new DateTime(year, month, day); } catch (ArgumentOutOfRangeException) { Birthdate = DateTime.Today; } } 

Some may disagree with exceptions like this, but I just let the DateTime class do its own checks, rather than recreate them on its own.

From the documentation , an ArgumentOutOfRangeException occurs if:

  • Year less than 1 or more than 9999, or
  • A month less than 1 or more than 12 or
  • A day is less than 1 or more than the number of days in a month.

Alternatively, you can copy the logic from the DateTime class: ( reference )

 public void setBirthdate(int year, int month, int day) { if (year >= 1 && year <= 9999 && month >= 1 && month <= 12) { int[] days = DateTime.IsLeapYear(year) ? new[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365} : new[] { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}; if (day >= 1 && day <= days[month] - days[month - 1]) Birthdate = new DateTime(year, month, day); } else Birthdate = DateTime.Today; } 
+5
source

I would use the TryParse ( MSDN ) method to exclude catch (which can be high overhead if often called with invalid values):

 DateTime date; if (DateTime.TryParse(string.Format("{0}-{1}-{2}", year, month, day), out date)) { // Date was valid. // date variable now contains a value. } else { // Date is not valid, default to today. date = DateTime.Today; } 
+2
source

Try the following:

 public void setBirthdate(int year, int month, int day) { try { Birthdate = new DateTime(year, month, day); } catch (Exception ex) { Birthdate = DateTime.Now; } } 
+1
source
 protected DateTime CheckDate(String date) { DateTime dt; try{ dt = DateTime.Parse(date); }catch(Exception ex){ dt = DateTime.now(); // may raise an exception } finally{ return dt; } } 
0
source

There is an error in the second part of Grant Winnie's code. The assignment after the IsLeapYear check is reversed. The correct code is:

  public static bool IsValidYearMonthDay(int year, int month, int day) { if (year >= 1 && year <= 9999 && month >= 1 && month <= 12) { int[] days = DateTime.IsLeapYear(year) ? new[] { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } : new[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; if (day >= 1 && day <= days[month] - days[month - 1]) { return true; } } return false; } 

And the test:

  [TestCase(2019, 10,21,true)] [TestCase(2019, 11, 31, false)] //november doesnt have 31, only 30 [TestCase(2016, 2, 29, true)] // is leap [TestCase(2014, 2, 29, false)] // is nop leap public static void ValidateYearMonthDay(int year, int month, int day, bool expectedresult) { var result = Date.IsValidYearMonthDay(year, month, day); Assert.AreEqual(expectedresult, result); } 
0
source

Source: https://habr.com/ru/post/980210/


All Articles