UNPKG

@gdjiami/jm-mrc-components

Version:

移动端可复用组件库

239 lines (238 loc) 7.4 kB
/** * 将日期相关的帮助方法抽离到jslib */ import Lunar from 'chinese-lunar'; import getHoliday from './chinessHoliday'; export var ChineseWeekDay = ['日', '一', '二', '三', '四', '五', '六']; export var Today = new Date(); export var ONE_WEEK = 7 * 24 * 3600 * 1000; var monthCache = {}; var defaultOptions = { lunar: true, holiday: true, }; export function genDates(date, option) { if (option === void 0) { option = defaultOptions; } // 缓存 var cacheKey = "" + date.getFullYear() + date.getMonth(); if (cacheKey in monthCache) { return monthCache[cacheKey]; } var arr = []; var time = new Date(date); // copy // 获取当月的第一天 time.setMonth(time.getMonth(), 1); // 当月第一天的星期数 var curFirstDay = time.getDay(); /** 获取上个月的最后几天 **/ // 更新为上个月的最后一天 time.setDate(0); var lastDayCount = time.getDate(); var tempYear = time.getFullYear(); var tempMonth = time.getMonth(); for (var i = curFirstDay; i > 0; i--) { var date_1 = lastDayCount - i + 1; var tempTime = new Date(tempYear, tempMonth, date_1); tempTime = startOfDate(tempTime); arr.push({ time: tempTime, id: tempTime.getTime(), value: date_1, isPass: true, isWeekDay: isWeekDay(tempTime), lunar: option.lunar ? Lunar.solarToLunar(tempTime, 'D') : undefined, holiday: option.holiday ? getHoliday(tempTime) : undefined, }); } /** 当月的日期数据 **/ // 移动到获取当月的最后一天 time.setMonth(time.getMonth() + 2, 0); // 当月的天数 var curDayCount = time.getDate(); tempYear = time.getFullYear(); tempMonth = time.getMonth(); for (var i = 1; i <= curDayCount; i++) { var tempTime = new Date(tempYear, tempMonth, i); tempTime = startOfDate(tempTime); arr.push({ time: tempTime, id: tempTime.getTime(), value: i, isWeekDay: isWeekDay(tempTime), lunar: option.lunar ? Lunar.solarToLunar(tempTime, 'D') : undefined, holiday: option.holiday ? getHoliday(tempTime) : undefined, }); } /** 下个月的前几天 **/ var count = arr.length <= 35 ? 35 : 42; var j = 1; var l = count - arr.length + 1; time.setMonth(tempMonth + 1, 1); tempYear = time.getFullYear(); tempMonth = time.getMonth(); while (j < l) { var tempTime = new Date(tempYear, tempMonth, j); tempTime = startOfDate(tempTime); arr.push({ time: tempTime, id: tempTime.getTime(), value: j, isFuture: true, isWeekDay: isWeekDay(tempTime), lunar: option.lunar ? Lunar.solarToLunar(tempTime, 'D') : undefined, holiday: option.holiday ? getHoliday(tempTime) : undefined, }); j++; } // freeze object return (monthCache[cacheKey] = arr); } export function isWeekDay(date) { var day = date.getDay(); return day != 0 && day != 6; } export function isSameYear(a, b) { return a.getFullYear() === b.getFullYear(); } export function isSameMonth(a, b) { return isSameYear(a, b) && a.getMonth() === b.getMonth(); } export function isSameDay(a, b) { return (a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate()); } export function isSameHour(a, b) { return isSameDay(a, b) && a.getHours() === b.getHours(); } export function isSameMinute(a, b) { return isSameHour(a, b) && a.getMinutes() === b.getMinutes(); } /** * 判断是否是闰年 * @param year */ export function isLeap(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } export function isToday(date) { return isSameDay(Today, date); } export function inRange(start, end, value) { if (end.getTime() < start.getTime()) { var temp = start; start = end; end = temp; } return value >= start && value <= end; } export function inRangeIgnoreTime(start, end, value) { if (end.getTime() < start.getTime()) { var temp = start; start = end; end = temp; } return (isSameDay(start, value) || isSameDay(end, value) || inRange(start, end, value)); } export function inMonth(month, value) { var start = new Date(month); start.setDate(1); start.setHours(0, 0, 0, 0); var end = new Date(start); end.setMonth(end.getMonth() + 1); end.setDate(0); end.setHours(23, 59, 59, 999); return inRange(start, end, value); } export function yesterday(date) { var copy = new Date(date); copy.setDate(copy.getDate() - 1); return copy; } export function tomorrow(date) { var copy = new Date(date); copy.setDate(copy.getDate() + 1); return copy; } export function startOfDate(date) { var copy = new Date(date); copy.setHours(0, 0, 0, 0); return copy; } export function endOfDate(date) { var copy = new Date(date); copy.setHours(23, 59, 59, 999); return copy; } export function midOfDate(date) { var copy = new Date(date); copy.setHours(12, 0, 0, 0); return copy; } export function outOfCurrentYear(date) { return date.getFullYear() !== Today.getFullYear(); } export function getLastDateOfMonth(date) { var copy = new Date(date); copy.setMonth(copy.getMonth() + 1); copy.setDate(0); return copy.getDate(); } export function afterHour(date, hour) { if (hour === void 0) { hour = 1; } var copy = new Date(date); copy.setHours(copy.getHours() + hour); return copy; } export function afterDay(date, day) { if (day === void 0) { day = 1; } var copy = new Date(date); copy.setDate(copy.getDate() + day); return copy; } export function afterMonth(date, month) { if (month === void 0) { month = 1; } var copy = new Date(date); copy.setDate(1); copy.setMonth(copy.getMonth() + month); var lastDate = getLastDateOfMonth(copy); if (date.getDate() > lastDate) { copy.setDate(lastDate); } else { copy.setDate(date.getDate()); } return copy; } export function afterYear(date, year) { if (year === void 0) { year = 1; } return afterMonth(date, year * 12); } export function monthDiff(a, b) { var yearDiff = a.getFullYear() - b.getFullYear(); var monthDiff = a.getMonth() - b.getMonth(); return yearDiff * 12 + monthDiff; } export function copyDate(from, to) { to.setFullYear(from.getFullYear()); to.setMonth(from.getMonth()); to.setDate(from.getDate()); return to; } export function copyTime(from, to) { to.setHours(from.getHours(), from.getMinutes(), from.getSeconds(), from.getMilliseconds()); return to; } export function roundHour(date) { var copy = new Date(date); var min = copy.getMinutes(); copy.setMinutes(0); copy.setSeconds(0); copy.setMilliseconds(0); if (min > 0) { copy.setHours(copy.getHours() + 1); } return copy; }