diff --git a/README.md b/README.md index f0fd8ab7..2dfe6cf7 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,6 @@ jQuery-QueryBuilder is available on [jsDelivr](https://www.jsdelivr.com/package/ * [Bootstrap 5](https://getbootstrap.com/docs/5.3/) CSS and bundle.js which includes `Popper` for tooltips and popovers * [Bootstrap Icons](https://icons.getbootstrap.com/) * [jQuery.extendext](https://github.com/mistic100/jQuery.extendext) - * [MomentJS](https://momentjs.com) (optional, for Date/Time validation) * [SQL Parser](https://github.com/mistic100/sql-parser) (optional, for SQL methods) * Other Bootstrap/jQuery plugins used by plugins diff --git a/package.json b/package.json index 4111f3a0..72b8dff4 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "bootstrap-icons": "^1.11.3", "jquery": "^3.5.1", "jquery-extendext": "^1.0.0", - "moment": "^2.29.1", + "dayjs": "^1.11.13", "sql-parser-mistic": "^1.2.3" }, "devDependencies": { diff --git a/src/data.js b/src/data.js index 7f466940..0a7f8de5 100644 --- a/src/data.js +++ b/src/data.js @@ -170,24 +170,28 @@ QueryBuilder.prototype._validateValue = function(rule, value) { // we need MomentJS if (validation.format) { - if (!('moment' in window)) { - Utils.error('MissingLibrary', 'MomentJS is required for Date/Time validation. Get it here http://momentjs.com'); - } - - var datetime = moment(tempValue[j], validation.format); + if (dayjs in window) { + window.momenty = window.dayjs; + } else if ('moment' in window) { + window.momenty = window.moment; + } else { + Utils.error('MissingLibrary', 'DayJS or MomentJS is required for Date/Time validation. Get it here https://day.js.org/ or https://momentjs.com'); + } + + var datetime = momenty(tempValue[j], validation.format); if (!datetime.isValid()) { result = [this.getValidationMessage(validation, 'format', 'datetime_invalid'), validation.format]; break; } else { if (validation.min) { - if (datetime < moment(validation.min, validation.format)) { + if (datetime < momenty(validation.min, validation.format)) { result = [this.getValidationMessage(validation, 'min', 'datetime_exceed_min'), validation.min]; break; } } if (validation.max) { - if (datetime > moment(validation.max, validation.format)) { + if (datetime > momenty(validation.max, validation.format)) { result = [this.getValidationMessage(validation, 'max', 'datetime_exceed_max'), validation.max]; break; } @@ -231,12 +235,17 @@ QueryBuilder.prototype._validateValue = function(rule, value) { case 'datetime': // we need MomentJS + if (validation.format) { - if (!('moment' in window)) { - Utils.error('MissingLibrary', 'MomentJS is required for Date/Time validation. Get it here http://momentjs.com'); - } - - if (moment(value[0], validation.format).isAfter(moment(value[1], validation.format))) { + if (dayjs in window) { + window.momenty = window.dayjs; + } else if ('moment' in window) { + window.momenty = window.moment; + } else { + Utils.error('MissingLibrary', 'DayJS or MomentJS is required for Date/Time validation. Get it here https://day.js.org/ or https://momentjs.com'); + } + + if (momenty(value[0], validation.format).isAfter(momenty(value[1], validation.format))) { result = ['datetime_between_invalid', value[0], value[1]]; } } diff --git a/tests/index.html b/tests/index.html index e322dcfd..717ec1fa 100644 --- a/tests/index.html +++ b/tests/index.html @@ -22,7 +22,7 @@ - + diff --git a/yarn.lock b/yarn.lock index 3a1b8f09..0281a846 100644 --- a/yarn.lock +++ b/yarn.lock @@ -766,7 +766,6 @@ foodoc@^0.0.9: handlebars-layouts "^3.1.4" jsdoc "^3.5.5" lunr "^1.0.0" - moment "^2.22.1" sanitize-html "^1.18.2" for-in@^1.0.1: @@ -1545,11 +1544,6 @@ mkdirp@^1.0.4: resolved "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz" integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== -moment@^2.22.1, moment@^2.29.1: - version "2.29.4" - resolved "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz" - integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w== - morgan@^1.10.0: version "1.10.0" resolved "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz"