How to validate user input date is the last day of the month using javascript?
-
How about a simple switch, case? – Shamim Hafiz - MSFT Jun 15 '11 at 08:39
-
4@Gunner: It's not necessarily all that simple. It's not *that* complex, if you stick to dates in the modern era, but even then you have the whole "leap year every four years except on centuries except do it on centuries that are evenly divisible by 400" thing to embed in your code and...well...the JavaScript engine already has it. :-) – T.J. Crowder Jun 15 '11 at 08:44
6 Answers
(Update: See the final example at the bottom, but the rest is left as background.)
You can add a day to the Date
instance and see if the month changes (because JavaScript's Date
object fixes up invalid day-of-month values intelligently), e.g.:
function isLastDay(dt) {
var test = new Date(dt.getTime()),
month = test.getMonth();
test.setDate(test.getDate() + 1);
return test.getMonth() !== month;
}
...or as paxdiablo pointed out, you can check the resulting day-of-month, which is probably faster (one fewer function call) and is definitely a bit shorter:
function isLastDay(dt) {
var test = new Date(dt.getTime());
test.setDate(test.getDate() + 1);
return test.getDate() === 1;
}
Another gratuitous live example
You could embed more logic in there to avoid creating the temporary date object if you liked since it's really only needed in February and the rest is just a table lookup, but the advantage of both of the above is that they defer all date math to the JavaScript engine. Creating the object is not going to be expensive enough to worry about.
...and finally: Since the JavaScript specification requires (Section 15.9.1.1) that a day is exactly 86,400,000 milliseconds long (when in reality days vary in length a bit), we can make the above even shorter by adding the day as we :
function isLastDay(dt) {
return new Date(dt.getTime() + 86400000).getDate() === 1;
}

- 1
- 1

- 1,031,962
- 187
- 1,923
- 1,875
-
2+1, similar to what I posted (albeit checking if the day-of-month was 1 rather than the month being different) but you beat me with the code so I deleted it :-) – paxdiablo Jun 15 '11 at 08:42
-
@paxdiablo: I was about to edit in your example...but you beat me to it. Thanks. – T.J. Crowder Jun 15 '11 at 08:48
-
@T.J. Crowder: This does not ensure it is the last day of the current month of the current year. – Shef Jun 15 '11 at 09:05
-
1@Shef: I'm not following you. What does the year have to do with it? – T.J. Crowder Jun 15 '11 at 09:07
-
@T.J. Crowder: Well, I believe the question was to validate the `input date` of the current month (notice date). What would your function return, if the user gave us `June 30, 2010 23:59:59`? Of course, it would return true, because `June 30, 2010 23:59:59` and `June 30, 2011 23:59:59` have the same last day of the month. – Shef Jun 15 '11 at 09:16
-
@Shef: The question, in total, is *"How to validate user input date is the last day of the month using javascript?"*. Nothing about worrying about what year it is. The user is giving them a date, and they want to know if it's the last day of the month. At least, that's how I read it. – T.J. Crowder Jun 15 '11 at 09:22
-
@T.J. Crowder: Hmm... well, it's not clear from the question, but maybe you are right. – Shef Jun 15 '11 at 09:25
Browsers identify the day 0 as the last day of the previous month
var month = 0; // January
var d = new Date(2008, month + 1, 0);
alert(d); // last day in January
as seen here : Calculate last day of month in javascript
you can simply use the month inserted by the user + 1 with day = 0 to check if he has inserted the last day of the month.
function isLastDayOfMonth(date){
return date.getDate() == new Date(date.getFullYear(),date.getMonth()+1,0).getDate();
}

- 501
- 4
- 12
If you want to avoid reinventing the wheel and having extra code to maintain and test, I highly suggest to use an external library like Moment.js or date-fns.
I personally advocate for date-fns as it is not as bulky as Moment and relies on Date primitives rather than Moment objects.
Your function of interest is named isLastDayOfMonth
and can be found here

- 1,616
- 2
- 24
- 51
Suppose that you got:
var month = getMonthFromUserInput();
var year = getYearFromUserInput();
This will give the last day of that month
new Date((new Date(year , month , 1)) -1 )
Remember that month 0 is Jan

- 708
- 6
- 17
This is worked.
var lastday = new Date(new Date().getFullYear(),new Date().getMonth()+1,0).getDate();
Hope help you.

- 676
- 1
- 8
- 24