Last day of the month
A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.
The task
Return the number of the last day of a month (28, 29, 30 or 31). `month` runs from 1 for January to 12 for December. Leap years follow the Gregorian rules: every 4 years, except century years, unless divisible by 400.
What this exercise teaches
Date.UTC(year, month, 0) asks for day 0 of the following month (months are 0-based in JavaScript), which rolls back to the last day of the requested month. The date engine applies the full leap-year rules for you.
A hint if you get stuck
Day 0 of the next month is the last day of this one, and `Date.UTC` rolls day 0 back for you. JavaScript months start at 0, which works in your favor with a 1-based `month`.
This exercise is part of Pro
Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.
You create a free account first, so Pro is attached to it.
Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.