1

I've tested this on the frontend of the imask (https://imask.js.org/) website, and can confirm that when you use a forward slash for the date formatting it refuses to allow the final digit of the date.

I have tried using:

IMask(
      field,
      {
        mask: Date,
        pattern: 'd{\/]}`m{\/}`Y',
        min: new Date(1990, 0, 1),
        max: new Date(2020, 0, 1),
        lazy: false
    });

IMask(
      field,
      {
        mask: Date,
        pattern: 'd{/]}`m{/}`Y',
        min: new Date(1990, 0, 1),
        max: new Date(2020, 0, 1),
        lazy: false
    });

IMask(
      field,
      {
        mask: Date,
        pattern: 'd/m/Y',
        min: new Date(1990, 0, 1),
        max: new Date(2020, 0, 1),
        lazy: false
    });

Anything but forward slashes works.

1 Answers1

1

Seems i found an answer.

IMask(
  date_of_birth_fields[i],
  {
    mask: Date,
    pattern: 'd/`m/`Y',
    format: function (date) {
      var day = date.getDate();
      var month = date.getMonth() + 1;
      var year = date.getFullYear();
  
      if (day < 10) day = "0" + day;
      if (month < 10) month = "0" + month;
  
      return [day, month, year].join('/');
    },
    // define str -> date convertion
    parse: function (str) {
      var yearMonthDay = str.split('/');
      return new Date(yearMonthDay[2], yearMonthDay[1] - 1, yearMonthDay[0]);
    },
    min: new Date(1980, 0, 1),
    max: new Date(2020, 0, 1),
    lazy: false,
});
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 20 '22 at 12:39
  • The docs state that "if you set pattern option, then you also have to provide format and parse options". I was struggling finding this information https://imask.js.org/guide.html#masked-date – Davide Carlier May 16 '23 at 12:07