From 4d7ae6ed9cd8e8b6bc0f9b3aea0d4b0a8e33c385 Mon Sep 17 00:00:00 2001 From: sight <26325820+Sight-wcg@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:35:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=AE=80=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/laydate.js | 78 +++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/src/modules/laydate.js b/src/modules/laydate.js index 2976832e..95099300 100644 --- a/src/modules/laydate.js +++ b/src/modules/laydate.js @@ -1087,6 +1087,64 @@ return that; }; + /** + * 给定年份的开始日期 + * @param {Date} date + */ + Class.prototype.startOfYear = function(date){ + var newDate = new Date(date); + newDate.setFullYear(newDate.getFullYear(), 0, 1); + newDate.setHours(0, 0, 0, 0); + return newDate; + } + + /** + * 给定年份的结束日期 + * @param {Date} date + */ + Class.prototype.endOfYear = function(date){ + var newDate = new Date(date); + var year = newDate.getFullYear(); + newDate.setFullYear(year + 1, 0, 0); + newDate.setHours(23, 59, 59, 999); + return newDate; + } + + /** + * 给定月份的开始日期 + * @param {Date} date + */ + Class.prototype.startOfMonth = function(date){ + var newDate = new Date(date); + newDate.setDate(1); + newDate.setHours(0, 0, 0, 0); + return newDate; + } + + /** + * 给定月份的结束日期 + * @param {Date} date + */ + Class.prototype.endOfMonth = function(date){ + var newDate = new Date(date); + var month = newDate.getMonth(); + newDate.setFullYear(newDate.getFullYear(), month + 1, 0); + newDate.setHours(23, 59, 59, 999); + return newDate; + } + + /** + * 将指定的天数添加到给定日期 + * @param {Date} date 要更改的日期 + * @param {number} amount 天数 + */ + Class.prototype.addDays = function(date, amount){ + var newDate = new Date(date); + if(!amount) return newDate; + newDate.setDate(newDate.getDate() + amount); + return newDate; + } + /** * @typedef limitOptions * @prop {JQuery} [elem] - 检测的元素, 例如面板中年月日时分秒元素,“现在”,“确认” 按钮等 @@ -1112,12 +1170,13 @@ var ONE_DAY = 24 * 60 * 60 * 1000; var isDisabledYearOrMonth = function(date, type){ - var startDay = type === 'year' ? new Date(date.year, 0, 1) : new Date(date.year, date.month, 1); // startOfYear/startOfMonth - var endDay = type === 'year' ? new Date(date.year, 11, 31) : new Date(date.year, date.month + 1, 0); // endOfYear/endOfMonth + var startDay = type === 'year' ? that.startOfYear(date) : that.startOfMonth(date); + var endDay = type === 'year' ? that.endOfYear(date) : that.endOfMonth(date); + var numOfDays = Math.floor((endDay.getTime() - startDay.getTime()) / ONE_DAY) + 1; var disabledDays = 0; for(var i = 0; i < numOfDays; i++){ - if(options.disabledDate.call(options, getFutureDate(startDay, i), position)){ + if(options.disabledDate.call(options, that.addDays(startDay, i), position)){ disabledDays++; } } @@ -1132,7 +1191,7 @@ return opts.type === 'year' || opts.type === 'month' ? isDisabledYearOrMonth(date, opts.type) - : options.disabledDate.call(options, that.newDate({year: date.year, month: date.month, date: date.date}), position); + : options.disabledDate.call(options, new Date(date), position); } var isDisabledItem = function(val, rangeFn){ @@ -1143,6 +1202,8 @@ if(!options.disabledTime) return; if(!(options.type === "time" || options.type === "datetime")) return; if(!(opts.disabledType === 'time' || opts.disabledType === 'datetime'))return; + + var date = that.systemDate(new Date(date)); var disabledTime = options.disabledTime.call(options, that.newDate(date), position); return opts.disabledType === 'datetime' @@ -1154,14 +1215,7 @@ isDisabledItem(date.seconds, disabledTime.disabledSeconds)][opts.time.length - 1]; } - function getFutureDate(date, days){ - var futureDate = new Date(date); - futureDate.setDate(futureDate.getDate() + days); - return futureDate; - } - - var datetimeObj = that.systemDate(new Date(currentDataTime)) - return isDisabledDate(datetimeObj) || isDisabledTime(datetimeObj); + return isDisabledDate(currentDataTime) || isDisabledTime(currentDataTime); }