import { conforms } from "lodash"; export const calcDate = (date1, date2) => { let date3 = date2 - date1; let days = Math.floor(date3 / (24 * 3600 * 1000)) let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数 let hours = Math.floor(leave1 / (3600 * 1000)) let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数 let minutes = Math.floor(leave2 / (60 * 1000)) let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数 let seconds = Math.round(date3 / 1000) return { leave1, leave2, leave3, days: days, hours: hours, minutes: minutes, seconds: seconds, } } /** * 日期格式化 */ export function dateFormat(date, format) { format = format || 'yyyy-MM-dd hh:mm:ss'; if (date !== 'Invalid Date') { let o = { "M+": date.getMonth() + 1, //month "d+": date.getDate(), //day "h+": date.getHours(), //hour "m+": date.getMinutes(), //minute "s+": date.getSeconds(), //second "q+": Math.floor((date.getMonth() + 3) / 3), //quarter "S": date.getMilliseconds() //millisecond } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (let k in o) if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); return format; } return ''; } /** * 获取上月第一天和下月最后一天 * type 1为 [yyyy-MM-dd,yyyy-MM-dd] * type 2为 [yyyy-MM-dd 00:00:00,yyyy-MM-dd 23:59:59] * type 4为 [yyyy-MM-dd,yyyy-MM-dd] 当月的第一天和最后一天 * type为空默认2 */ export function defaultDate(type) { type = type ? type : 2 const date = new Date(); const startDate = new Date(date.getFullYear(), date.getMonth() - 1, 1); const endDate = new Date(date.getFullYear(), date.getMonth() + 2, 0); if (type == 1) { return [ dateFormat(startDate, "yyyy-MM-dd"), dateFormat(endDate, "yyyy-MM-dd") ]; } else if (type == 3) { return [ dateFormat(new Date(date.getFullYear(), date.getMonth() - 1, 1), "yyyy-MM-dd"), dateFormat(new Date((date.getMonth() + 1) === 12 ? date.getFullYear() + 1 : date.getFullYear(), (date.getMonth() + 1), date.getDate()), "yyyy-MM-dd") ]; } else if (type == 4) { return [ dateFormat(new Date(date.getFullYear(), date.getMonth(), 1), "yyyy-MM-dd"), dateFormat(new Date(date.getFullYear(), date.getMonth() + 1, 0), "yyyy-MM-dd") ]; } else { return [ dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00", dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59" ]; } } /** * 获取当天 * [yyyy-MM-dd,yyyy-MM-dd] */ export function defaultDate2() { const date = new Date(); return [ dateFormat(date, "yyyy-MM-dd"), dateFormat(date, "yyyy-MM-dd") ]; } /** * 获取本月第一天和本月最后一天 * [yyyy-MM-dd,yyyy-MM-dd] */ export function defaultDate3() { const date = new Date(); const startDate = new Date(date.getFullYear(), date.getMonth(), 1); const endDate = new Date(date.getFullYear(), date.getMonth() + 1, 0); return [ dateFormat(startDate, "yyyy-MM-dd"), dateFormat(endDate, "yyyy-MM-dd") ]; } /** * 获取本月第一天 * [yyyy-MM-dd,yyyy-MM-dd] */ export function startDate() { const date = new Date(); const startDate = new Date(date.getFullYear(), date.getMonth(), 1); return dateFormat(startDate, "yyyy-MM-dd"); } /** * 获取本月最后一天 * [yyyy-MM-dd,yyyy-MM-dd] */ export function endDate() { const date = new Date(); const endDate = new Date(date.getFullYear(), date.getMonth() + 1, 0); return dateFormat(endDate, "yyyy-MM-dd"); } /** * 获取当年 * [yyyy-MM-dd,yyyy-MM-dd] */ export function defaultDate4() { const date = new Date(); return dateFormat(date, "yyyy"); } /** * 获取本年上个月 */ export function CurrentMonth(date, format = 'yyyy-MM-dd hh:mm:ss') { format = format || 'yyyy-MM-dd hh:mm:ss'; if (date !== 'Invalid Date') { let o = { "M+": date.getMonth(), //month "d+": date.getDate(), //day "h+": date.getHours(), //hour "m+": date.getMinutes(), //minute "s+": date.getSeconds(), //second "q+": Math.floor((date.getMonth() + 3) / 3), //quarter "S": date.getMilliseconds() //millisecond } if (Number(o["M+"]) == 0) { o["M+"] = 1 } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (let k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; } return ''; } /** * 获取上月第一天和上月最后一天 * [yyyy-MM-dd,yyyy-MM-dd] */ export function defaultDate5() { const date = new Date(); const startDate = new Date(date.getFullYear(), date.getMonth() - 1, 1); const endDate = new Date(date.getFullYear(), date.getMonth(), 0); return [ dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00", dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59" ]; } /** * 获取本月第一天和本月最后一天 * [yyyy-MM-dd,yyyy-MM-dd] */ export function defaultDate6() { const date = new Date(); const startDate = new Date(date.getFullYear(), date.getMonth(), 1); const endDate = new Date(date.getFullYear(), date.getMonth() + 1, 0); return [ dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00", dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59" ]; } // 获得当前日期 export function getCurrentDate(type = 'dateTime') { const date = new Date(); const currentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate()); if (type == 'date') { return dateFormat(currentDate, 'yyyy-MM-dd') } else if (type == 'dateTime') { return dateFormat(date, 'yyyy-MM-dd hh:mm:ss') } } export function getYearDate() { const date = new Date(); return date.getFullYear() } export function getMonthDate() { const date = new Date(); return date.getMonth() + 1 } /** * 获取 n 天后的日期 * yyyy-MM-dd */ export const NdayDate = (num) => { let date1 = new Date(); //今天时间 let time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); let date2 = new Date(date1); date2.setDate(date1.getDate() + num); //num是正数表示之后的时间,num负数表示之前的时间,0表示今天 let time2 = addZero(date2.getFullYear()) + "-" + addZero((date2.getMonth() + 1)) + "-" + addZero(date2.getDate()); return time2; } // 补零方法 function addZero(num) { if (parseInt(num) < 10) { num = '0' + num; } return num; } //验证英文,允许 TAB 存在 export function verifyEnglish(str) { const regex = /^[A-Za-z0-9\r\n\t .,!?:()|/+-_;'"$&@%*#]+$/; return regex.test(str ? str : null); } //验证符号 export function verifySymbol(str) { const regex = /^[A-Za-z0-9\r\n .,!?:()|/+-_;'"$&@%*#]+$/; return regex.test(str ? str : null); } // 匹配常见全角符号的正则表达式(包括标点、字母、数字等) export function checkFullWidthSymbols_bak(text) { if (text) { const fullWidthRegex = /[\u3000-\u303F\uFF01-\uFF5E\uFFE0-\uFFE5]/g; const matchesItem = []; let lines = text.split('\n') let match; lines.forEach((ln, index, array) => { while ((match = fullWidthRegex.exec(ln)) !== null) { matchesItem.push({ row: index + 1, // 行号 index: match.index, // 符号的位置 symbol: match[0] // 符号本身 }); } }) let matches = []; matches = matchesItem.reduce((acc, item) => { const existingItem = acc.find(i => i.row === item.row); if (existingItem) { existingItem.symbol += ` ${item.symbol}`; // 合并值 } else { acc.push({ ...item }); // 添加新项 } return acc; }, []); return { hasFullWidth: matches.length > 0, // 是否包含全角符号 positions: matches // 符号的详细位置和内容 }; } else { return { hasFullWidth: false, // 是否包含全角符号 positions: [] // 符号的详细位置和内容 }; } } // wfg export function checkFullWidthSymbols(text) { if (text) { const fullWidthRegex = /[\u4e00-\u9fa5\u3000-\u303f\uff00-\uffef\u3400-\u4dbf]/g; const matchesItem = []; // TAB 键转为空格 let lines = text .trimEnd() .replaceAll("\t", " ") .split("\n"); let match; lines.forEach((ln, index, array) => { ln = ln.trimEnd(); lines[index] = ln; while ((match = fullWidthRegex.exec(ln)) !== null) { matchesItem.push({ row: index + 1, // 行号 index: match.index, // 符号的位置 symbol: match[0], // 符号本身 input: match.input // 行 }); } }); let resetText = lines.join("\n"); let isResetText = resetText != text; let message = []; matchesItem.forEach(m => { message.push(`第 ${m.row} 行第 ${m.index} 个字符 ${m.symbol} 不符合要求,存在全角字符或汉字等!`); }); return { hasFullWidth: matchesItem.length > 0, // 是否包含全角符号 positions: matchesItem, // 符号的详细位置和内容 message: message.join("
"), isResetText: isResetText, // 是否重设文本 resetText: resetText // 重设的文本 }; } else { return { hasFullWidth: false, // 是否包含全角符号 positions: [], // 符号的详细位置和内容 message: "", isResetText: false, // 是否重设文本 resetText: "" // 重设的文本 }; } } //判断是否超过设置天数 export function overdueJudgment(date1, date2) { let lockTermGlobal = localStorage.getItem("lockTermGlobal"); if (lockTermGlobal === "1") { const overDay = localStorage.getItem("lockTermDays"); console.log("overdueJudgment", overDay, date1, date2); if (overDay) { let r = false; const nowDate = new Date(dateFormat(new Date(), "yyyy-MM-dd")); const startDate1 = new Date(dateFormat(new Date(date1), "yyyy-MM-dd")); startDate1.setDate(startDate1.getDate() + Number(overDay)); r = startDate1 < nowDate; if (r === true && date2) { const overDay2 = localStorage.getItem("secondLockTermDays"); if(overDay2) { console.log("overdueJudgment, date2", overDay2); const startDate2 = new Date(dateFormat(new Date(date2), "yyyy-MM-dd")); startDate2.setDate(startDate2.getDate() + Number(overDay2)); r = startDate2 < nowDate; } } return r; } else { return false; } }else{ return false; } }