简体   繁体   中英

How to get an european date checker in JS?

so i have a box where you can set a date

        <div class="field">
          <label class="main">Date:</label>
          <input name="date" type="text" placeholder="dd/mm/yyyy">
          <span class="error"></span>
        </div>

As you can see I make use of an input name="date", whitch is an JS file with the following code:

date: function(input, value, format) {

userFormat = userFormat || 'mm/dd/yyyy';

var delimiter = /[^mdy]/.exec(userFormat)[0];

var theFormat = userFormat.split(delimiter);

var theDate = value.split(delimiter);

function isDate(date, format) {
 var m, d, y, i = 0, len = format.length, f;
 for (i; i < len; i++) {
  f = format[i];
  if (/m/.test(f)) m = date[i];
  if (/d/.test(f)) d = date[i];
  if (/y/.test(f)) y = date[i];
}
return (
  m > 0 && m < 13 &&
  y && y.length === 4 &&
  d > 0 &&
  // Check if it's a valid day of the month
  d <= (new Date(y, m, 0)).getDate()
);
}

return isDate(theDate, theFormat);
}

The problem is, I can only use mm/dd/yyyy as input and not dd/mm/yyy.. Any idea how to fix this?

I got it! The only thing I had to do was to change:

userFormat = userFormat || 'mm/dd/yyyy';

into:

userFormat = userFormat || 'dd/mm/yyyy';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM