| | |
| | | } |
| | | return workMonth |
| | | } |
| | | |
| | | /** |
| | | * 判断时间区间是否超过 1 个自然月 |
| | | * @param {string} startTime - 开始时间 yyyy-MM-dd HH:mm:ss |
| | | * @param {string} endTime - 结束时间 yyyy-MM-dd HH:mm:ss |
| | | * @returns {boolean} true=超过一个月,false=未超过 |
| | | */ |
| | | export function isOverOneMonth(startTime, endTime) { |
| | | // 1. 解析为日期对象 |
| | | const start = new Date(startTime); |
| | | const end = new Date(endTime); |
| | | |
| | | // 2. 校验日期合法性 |
| | | if (isNaN(start.getTime()) || isNaN(end.getTime())) { |
| | | throw new Error("时间格式错误,请使用 yyyy-MM-dd HH:mm:ss 格式"); |
| | | } |
| | | |
| | | // 3. 如果结束时间早于开始时间,直接返回false |
| | | if (end < start) return false; |
| | | |
| | | // 4. 计算年份差、月份差 |
| | | const startYear = start.getFullYear(); |
| | | const startMonth = start.getMonth(); |
| | | const startDay = start.getDate(); |
| | | const startRest = start.getTime() - new Date(startYear, startMonth, startDay).getTime(); // 时分秒毫秒 |
| | | |
| | | const endYear = end.getFullYear(); |
| | | const endMonth = end.getMonth(); |
| | | const endDay = end.getDate(); |
| | | const endRest = end.getTime() - new Date(endYear, endMonth, endDay).getTime(); |
| | | |
| | | // 总月份差值 |
| | | const monthDiff = (endYear - startYear) * 12 + (endMonth - startMonth); |
| | | |
| | | // 5. 判断逻辑 |
| | | if (monthDiff > 1) { |
| | | return true; // 月份差>1 → 超过 |
| | | } else if (monthDiff === 1) { |
| | | // 月份差=1 → 比较 日+时分秒,超过则判定超时 |
| | | return endDay > startDay || (endDay === startDay && endRest > startRest); |
| | | } else { |
| | | return false; // 月份差<1 → 未超过 |
| | | } |
| | | } |