How do you know if a month has 30 or 31 days javascript?
Show Brett DeWoody 56.9k28 gold badges135 silver badges183 bronze badges asked Jul 26, 2009 at 11:27
1
answered Jul 26, 2009 at 11:36 16
answered Jul 26, 2009 at 22:57
kennebeckennebec 100k31 gold badges104 silver badges126 bronze badges 3 The following takes any valid datetime value and returns the number of days in the associated month... it eliminates the ambiguity of both other answers...
Biruk Abebe 2,2041 gold badge11 silver badges24 bronze badges answered Jul 26, 2009 at 17:28
Charles BretanaCharles Bretana 140k22 gold badges145 silver badges213 bronze badges 7 Another possible option would be to use Datejs Then you can do
Although adding a library just for this function is overkill, it's always nice to know all the options you have available to you :)
yckart 30.9k9 gold badges117 silver badges127 bronze badges answered Sep 23, 2009 at 8:30
RYFNRYFN 2,8671 gold badge29 silver badges37 bronze badges 2 Get the Number of Days in a Month #To get the number of days in a month:
We passed the following 3 arguments to the new Date() constructor:
If you're passing the result of calling
Passing For
example, passing a month index of
This returns an object that represents the last day of the month. The last step is to call the Date.getDate method. The method returns an integer from Getting the integer representation of the last day of the month is the equivalent of getting the number of days in the month. Further Reading #
How do you know if a month has 31 days JavaScript?getDate method. The method returns an integer from 1 to 31 , which represents the day of the month for a given date. Getting the integer representation of the last day of the month is the equivalent of getting the number of days in the month.
How do I know if a date is within 30 days?To check if a date is within 30 days:. Subtract the timestamp of the current date from the timestamp of the date.. Pass the result to the Math. abs() function.. Convert the result to days.. Check if the days between the dates is less than 30 .. How do you get no of days in a month in JS?Get Number of Days in Month Using JavaScript. const getDays = (year, month) => { return new Date(year, month, 0). getDate(); };. const daysInSeptember = getDays(2021, 7); // Returns 31.. const daysInSeptember = getDays(new Date(). getFullYear(), 7); // Returns 31.. |