Member-only story
Dates are one of those parts in JavaScript that are notoriously bad. Functions are inconsistent, and often you get some weird results. Just looking at the date constructor, you can notice many issues. If you pass an integer as a parameter, it is a timestamp. Converting that integer to string makes it year parameter. There are many more, and in this post, I am covering some of them. Then I am showing an example of how to validate that date had the correct value.
Issues with Date class
Constructor and single parameter
As previously mentioned, the constructor treats the first parameter differently based on the type and number of parameters passed. When the constructor receives only one number as a parameter, this a timestamp. If that parameter is a string, then it is a year. When having multiple number parameters, they represent date elements — first representing year, the second month, and third, being the day of the month.
Month values
Months in date objects are a special case. While all values start from one, months start from zero. So if you are using the setMonth method and want to set it to January, you need to pass value zero. That again doesn’t work for all use cases. If you are creating a date instance using ISO date format, than for January, you need…