From a38f54f02b48cf8ef2c9ed6febed560b4f38d534 Mon Sep 17 00:00:00 2001 From: wjt <1797368093@qq.com> Date: 星期五, 21 六月 2024 15:26:42 +0800 Subject: [PATCH] Merge branch 'master' of http://218.28.192.34:9999/r/sqys/sqys_xcx --- components/lingfeng-timepicker/uitls/util.js | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 163 insertions(+), 0 deletions(-) diff --git a/components/lingfeng-timepicker/uitls/util.js b/components/lingfeng-timepicker/uitls/util.js new file mode 100644 index 0000000..976744b --- /dev/null +++ b/components/lingfeng-timepicker/uitls/util.js @@ -0,0 +1,163 @@ +/** + * 鑾峰彇鏌愬勾鏌愭湀鏈夊灏戝ぉ + */ +export const getOneMonthDays = (year, month) => { + month = Number(month); + const baseMonthsDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { + if (month === 1) { + baseMonthsDays[month] = 29; + } + } + return baseMonthsDays[month]; +} + +/** + * 鑾峰彇鏃ユ湡鐨勫勾鏈堟棩鏃跺垎绉� + */ +export const getTimeArray = (data) => { + const year = data.getFullYear(); + const month = data.getMonth() + 1; + const date = data.getDate(); + const hour = data.getHours(); + const minute = data.getMinutes(); + const second = data.getSeconds(); + return [year, month, date, hour, minute, second]; +} +/** + * 灏忎簬10鐨勬暟瀛楀墠闈㈣ˉ0 + */ +export const addZero = (num) => { + if (num / 1 == 0) { + return "00"; + } + num = "" + num; + return num < 10 ? (num.substr(0, 1) == 0 ? num : ('0' + num)) : num; +} +/** + * 灏忎簬10鐨勬暟瀛楀墠闈㈠幓闄�0 + */ +export const removeZero = (num) => { + if (num / 1 == 0) { + return 0 + } + num = "" + num; + return num.substr(0, 1) == 0 ? (num.substr(1)) : num; +} +/** + * 鑾峰彇褰撳墠鍊煎湪鏁扮粍涓殑绱㈠紩 + */ +export const getIndexOfArray = (value, array, key) => { + let index; + if (key) { + index = array.findIndex(item => item[key] == value); + } else { + index = array.findIndex(item => item == value); + } + return index > -1 ? index : 0; +} + +/** + * 鑾峰彇瀛e害 + */ +export const getQuarterArray = (startMOnth, endMOnth) => { + let arr = ["涓�瀛e害", "浜屽搴�", "涓夊搴�", "鍥涘搴�"]; + let start = Math.ceil(startMOnth / 3); + let end = Math.ceil(endMOnth / 3); + if (end < start) { + return arr; + } else { + return arr.slice(start - 1, end); + } +} +/** + * 鏄惁涓鸿寖鍥撮�夋嫨 + */ +export const isRange = (type) => { + return type.indexOf("range") > -1; +} +/** + * 鏄惁浠呬负鏃堕棿閫夋嫨 + */ +export const isOnlyTime = (type) => { + return ["time", "hour-minute", "time-range"].includes(type); +} +/** + * 鑾峰彇start,end涔嬮棿鏈夊灏戝懆 + */ +export const getTotalWeeks = (start, end, en, weekType) => { + if (weekType === 'firstDay') { + return getFirstDayTotalWeeks(start, end, weekType) + } + //鑾峰彇end褰撳墠鍛ㄧ殑绗竴澶� + let endMon = getWeekFirstDate(new Date(end), en); + //鑾峰彇start褰撳墠鍛ㄧ殑绗竴澶� + let startMon = getWeekFirstDate(new Date(start), en); + let year = new Date(start).getFullYear(); + let firMon = getWeekFirstDate(new Date(year + '/01/01'), en); + if (weekType === 'fullWeek') { + if (new Date(startMon).getFullYear() != year) { + let curTime = new Date(startMon); + startMon = curTime.setDate(curTime.getDate() + 7); + } + if (new Date(firMon).getFullYear() != year) { + let curTime = new Date(firMon); + firMon = curTime.setDate(curTime.getDate() + 7); + } + } + return getStartAndEndWeek(firMon, startMon, endMon, weekType); +} +/** + * 鑾峰彇褰撳墠鍛ㄧ殑绗竴澶� + * 榛樿鍛ㄤ竴涓虹涓�澶╋紝en=true 鏃跺懆鏃ヤ负绗竴澶� + */ +function getWeekFirstDate(date, en = false) { + let temptTime = new Date(date); + let weekday = temptTime.getDay() || (en ? 0 : 7); + return temptTime.setDate(temptTime.getDate() - weekday + (en ? 0 : 1)); +} +/** + * 鑾峰彇褰撳墠骞寸week鍛ㄧ殑绗竴澶╁拰鏈�鍚庝竴澶� + * 榛樿鍛ㄤ竴涓虹涓�澶╋紝en=true 鏃跺懆鏃ヤ负绗竴澶� + */ +export const getFirstAndLastDate = (year, week, en, weekType) => { + let firstDate = new Date(getWeekFirstDate(new Date(year + '/01/01'), en)); + if (weekType === 'fullWeek') { + if (firstDate.getFullYear() != year) { + firstDate.setDate(firstDate.getDate() + 7); + } + } + if (weekType === 'firstDay') { + firstDate = new Date(year + '/01/01'); + } + firstDate = new Date(firstDate.setDate(firstDate.getDate() + (week - 1) * 7)); + let lastDate = new Date(firstDate); + lastDate = new Date(lastDate.setDate(lastDate.getDate() + 6)); + const [fy, fm, fd] = getTimeArray(firstDate); + const [ly, lm, ld] = getTimeArray(lastDate); + const start = `${fy}-${addZero(fm)}-${addZero(fd)}`; + const end = `${ly}-${addZero(lm)}-${addZero(ld)}`; + return { + start, + end + }; +} + +function getStartAndEndWeek(first, start, end) { + let d = Math.ceil((end.valueOf() - first.valueOf()) / 8.64e7) + 1; + let endWeek = Math.ceil(d / 7); + let startWeek = 1; + if (start !== first) { + let d = Math.ceil((start.valueOf() - first.valueOf()) / 8.64e7) + 1; + startWeek = Math.ceil(d / 7); + } + return [startWeek, endWeek]; +} + +function getFirstDayTotalWeeks(start, end, weekType) { + let year = new Date(start).getFullYear(); + let startTime = new Date(start).getTime(); + let endTime = new Date(end).getTime(); + let firstTime = new Date(year + '/01/01').getTime(); + return getStartAndEndWeek(firstTime, startTime, endTime, weekType) +} \ No newline at end of file -- Gitblit v1.9.1