Daler Daler - 2 years ago 91
Javascript Question

How to set the max and min limit year for moment.js

I do use

to parse the date. So far, it does parse the date valid, but after reading the documentation I didn't find how to set some logical checks. What I have now:

var death_day_moment = moment(death, ["DD-MM-YYYY"]);

var dmy = death_day_moment.invalidAt();
switch(dmy) {
case 0:
alert("The year is wrong");

case 1:
alert("The month is wrong);

case 2:
alert("The day is wrong");

It does work, overall, it does validate if its date or no. But I want to limit the date to current max date (so they cant put the future, tomorrow's date) for example. How would one do it?

Examples, lest say today is 23--05-2017 (current date):


01-01-2017 -- Correct

01-12-2016 -- Incorrect as its from previous year

01-06-2017 -- Incorrect as its the future date.

02-02-2017 -- Correct

Edit1: I have moment.js version 2.5

Answer Source

You can simply use isBetween

Check if a moment is between two other moments, optionally looking at unit scale (minutes, hours, days, etc). The match is exclusive.

But you have to update to moment 2.9.0+

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download