MrBuggy MrBuggy - 1 year ago 93
Javascript Question

Don't allow convert to future year moment.js

I'm using moment.js to handle my date time values in my web application. I'm using follow format:

for example
. Now I have some input fields, where I can type in the day of birth of a person. When I type a short year between 62 and 99, it converts it like this for example:
it converts it after pressing tab to
(puts 20 befor the 82). When I write a short year between 00 and 62 in converts it like this example:
would be converted to
(puts 19 before the 55). So the first range will be convertet to a year in the 21th century and the second range would be convertet to a year in the 20th century (this seems to be default, I don't know why). Now I would like to prevent converting to a year, which is in the future (so not more than 2016, and just 2016 when the short year is 16). How can I do this? At this moment I have something like this:

if(value.isValid() && isDayOfBirth) {

Is there a way to do this or to check it? I didn't found anything in the Thanks.

Answer Source

This should work. You take the current year and check if the parsed year is greater than that. If so, you subtract 100 years (bringing you back to 20th century).

var now = moment(),
    year = now.year(); //Get current year

if (parsedDate.year() > year) {
  parsedDate.subtract(100, "year");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download