/** * Independent time operation tool to facilitate subsequent switch to dayjs */ // TODO 芋艿:【锁屏】可能后面删除掉 import dayjs from 'dayjs' const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss' const DATE_FORMAT = 'YYYY-MM-DD' const DATE_FORMAT2 = 'YYYY-MM' export function formatToDateTime(date?: dayjs.ConfigType, format = DATE_TIME_FORMAT): string { return dayjs(date).format(format) } export function formatToDate(date?: dayjs.ConfigType, format = DATE_FORMAT): string { return dayjs(date).format(format) } export function formatToDate2(date?: dayjs.ConfigType, format = DATE_FORMAT2): string { return dayjs(date).format(format) } export function getMonthStartAndEndSimplified(date = new Date()) { const startDate = new Date(date) startDate.setDate(1) const year = startDate.getFullYear() const month = startDate.getMonth() + 1 const formattedStartDate = startDate.toISOString().split('T')[0] const endDate = new Date(year, month, 0) // 直接利用月份超出范围的特性 endDate.setDate(endDate.getDate()) // 这行代码其实可以省略,因为endDate已经是最后一天了 const formattedEndDate = endDate.toISOString().split('T')[0] return [formattedStartDate, formattedEndDate] } export function getWeekStartAndEnd(date = new Date()) { // 复制传入的日期对象 const startDate = new Date(date) // 获取当前日期是周几(0代表周日,1代表周一,...,6代表周六) const dayOfWeek = startDate.getDay() // 计算本周一的日期 // 如果今天是周一(dayOfWeek === 1),则不需要调整 // 如果不是,则需要向前移动到周一 startDate.setDate(startDate.getDate() - dayOfWeek + 1) // 创建结束日期(周日) // 本周日就是本周一加上6天 const endDate = new Date(startDate) endDate.setDate(endDate.getDate() + 6) // 格式化日期为 YYYY-MM-DD function formatDate(d: any) { const year = d.getFullYear() const month = String(d.getMonth() + 1).padStart(2, '0') // 月份从0开始,所以+1 const day = String(d.getDate()).padStart(2, '0') return `${year}-${month}-${day}` } // 返回格式化后的开始日期和结束日期 return [formatDate(startDate), formatDate(endDate)] } export function getYearStartAndEnd(date = new Date()) { // 获取当前年份 const year = date.getFullYear() // 创建本年的开始日期(1月1日) const startDate = new Date(year, 0, 1) // 月份是从0开始的,所以0代表1月 // 创建本年的结束日期(12月31日) const endDate = new Date(year, 11, 31) // 11代表12月 // 格式化日期为 YYYY-MM-DD function formatDate(d) { const year = d.getFullYear() const month = String(d.getMonth() + 1).padStart(2, '0') // 月份从0开始,所以+1 const day = String(d.getDate()).padStart(2, '0') return `${year}-${month}-${day}` } // 返回格式化后的开始日期和结束日期 return [formatDate(startDate), formatDate(endDate)] } /** * 格式化日期为YYYY-MM-DD格式 * @param {Date} date - 要格式化的日期对象,默认为当前日期 * @returns {string} 格式化后的日期字符串 */ export function formatDateYYMMDD (date = new Date()){ const year = date.getFullYear(); // 月份从0开始,需要+1并补零 const month = String(date.getMonth() + 1).padStart(2, '0'); // 日期补零 const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } /** * 比较两个时间字符串的大小 * @param {string} time1 - 第一个时间字符串,格式为HH:mm * @param {string} time2 - 第二个时间字符串,格式为HH:mm * @returns {number} - 如果time1 > time2返回1,如果time1 < time2返回-1,如果相等返回0 */ export function compareTimeStrings(time1, time2) { // 解析时间字符串为分钟数 const convertToMinutes = (timeStr) => { const [hours, minutes] = timeStr.split(':').map(Number); return hours * 60 + minutes; }; const minutes1 = convertToMinutes(time1); const minutes2 = convertToMinutes(time2); // 比较大小 if (minutes1 > minutes2) { return 1; } else if (minutes1 < minutes2) { return -1; } else { return 0; } } // 检查时间格式的辅助函数 export function isValidTimeFormat(timeStr) { const regex = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/; return regex.test(timeStr); } const chineseNumbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; //const chineseUnits = ['', '十', '百', '千']; //const chineseBigUnits = ['', '万', '亿']; /** * 数字转中文工具类 */ export function NumberToChineseUtils (num:number){ // 中文数字映射表 if (num < 1 || num > 20) { throw new Error('目前仅支持1-20的数字转换'); } if (num < 10) { return chineseNumbers[num]; } if (num === 10) { return '十'; } if (num < 20) { return '十' + chineseNumbers[num % 10]; } if (num === 20) { return '二十'; } return ''; } /** * 获取当前月份的第一天和最后一天 * @param date - 日期对象,默认为当前日期 * @returns 包含第一天和最后一天的字符串数组,格式为 [YYYY-MM-DD, YYYY-MM-DD] */ export function getCurrentMonthRange(date = new Date()) { const year = date.getFullYear(); const month = date.getMonth(); // 0-11 // 本月第一天 const firstDay = new Date(year, month, 1); // 本月最后一天 const lastDay = new Date(year, month + 1, 0); // 格式化为 YYYY-MM-DD 字符串 const formatToYYYYMMDD = (d: Date) => { const year = d.getFullYear(); const month = String(d.getMonth() + 1).padStart(2, '0'); const day = String(d.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }; return [formatToYYYYMMDD(firstDay), formatToYYYYMMDD(lastDay)]; } /** * 获取当前月份的第一天和最后一天 * @param date - 日期对象,默认为当前日期 * @returns 包含第一天和最后一天的字符串数组,格式为 [YYYY-MM-DD, YYYY-MM-DD] */ export function getCurrentMonthRange_1(date = new Date()) { const year = date.getFullYear(); const month = date.getMonth(); // 0-11 // 本月第一天 const firstDay = new Date(year, month, 1); // 本月最后一天 const lastDay = new Date(year, month + 1, 1); // 格式化为 YYYY-MM-DD 字符串 const formatToYYYYMM = (d: Date) => { const year = d.getFullYear(); const month = String(d.getMonth() + 1).padStart(2, '0'); return `${year}-${month}`; }; return [formatToYYYYMM(firstDay), formatToYYYYMM(lastDay)]; } /** * 将 13 位时间戳转换为指定格式的日期字符串 * @param timestamp - 13 位时间戳 * @param format - 日期格式,默认为 'YYYY-MM-DD HH:mm:ss' * @returns 格式化后的日期字符串 */ export function formatTimestamp(timestamp: number, format = DATE_TIME_FORMAT): string { // 确保时间戳是 13 位,如果不是则转换 const timestamp13 = timestamp.toString().length === 13 ? timestamp : timestamp * 1000; return dayjs(timestamp13).format(format); } /** * 将 13 位时间戳转换为指定格式的日期字符串 * @param timestamp - 13 位时间戳 * @param format - 日期格式,默认为 'YYYY-MM-DD HH:mm:ss' * @returns 格式化后的日期字符串 */ export function formatTimestampYMD(timestamp: number, format = DATE_FORMAT): string { // 确保时间戳是 13 位,如果不是则转换 const timestamp13 = timestamp.toString().length === 13 ? timestamp : timestamp * 1000; return dayjs(timestamp13).format(format); } export const dateUtil = dayjs