1

I let a user to select a day of month, a month itself and a year. But I need to check if the date correct. For example, it's not 29th of February when the year is not leap. Or it's not 31st of September because September has only 30 days. For now I have a complex if/else logic for that to check is the year leap or not. Then check the month to get the maximumamount of days it can have. But is there more 'elegant' way to do that? Maybe some library?

Ingo Steinke
  • 766
  • 3
  • 11
  • 30
scripter2021
  • 341
  • 2
  • 10
  • Perhaps Moment.js ? – zipzit Oct 19 '21 at 16:06
  • Suggested to edit the question, because asking for any "best" solution is considered to attract opinionated answers on StackOverflow, as "best" is ambiguous (fastest? most elegant? robust? popular?) so we have to elaborate which kind of best we mean – Ingo Steinke Oct 19 '21 at 16:08
  • [Date()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) – Ingo Steinke Oct 19 '21 at 16:10
  • 1
    The question was closed before I could submit my answer. You can create a Date object and verify its actual day. If JavaScript had to "fix" the date, it wasn't valid in the first place. – Álvaro González Oct 19 '21 at 16:15
  • @ÁlvaroGonzález cool! thanks! – scripter2021 Oct 19 '21 at 16:37

0 Answers0