// 时间戳转换为年月日时分秒 export const timesTampChange = (timestamp, format = 'Y-M-D h:m:s') => { if (!timestamp) return '' const date = new Date(timestamp) const Y = date.getFullYear().toString() const M = (date.getMonth() + 1).toString().padStart(2, '0') const D = date.getDate().toString().padStart(2, '0') const h = date.getHours().toString().padStart(2, '0') const m = date.getMinutes().toString().padStart(2, '0') const s = date.getSeconds().toString().padStart(2, '0') const formatter = { 'Y': Y, 'M': M, 'D': D, 'h': h, 'm': m, 's': s } // 替换format中的占位符 let formattedDate = format.replace(/Y|M|D|h|m|s/g, matched => formatter[matched]) // 使用正则表达式匹配并替换占位符 if (!formattedDate) formattedDate = Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s return formattedDate } // 根据生日时间戳计算年龄 export const getAgeByBirthdayTimestamp = (timestamp) => { const now = new Date() const birthday = new Date(timestamp) const age = now.getFullYear() - birthday.getFullYear() return age } // 将YYYY-MM转换为时间戳 export const convertYearMonthToTimestamp = (yearMonth) => { const regex = /^\d{4}-\d{2}$/ if (!regex.test(yearMonth)) { throw new Error("Invalid input format. Expected 'YYYY-MM'.") } const [year, month] = yearMonth.split('-').map(Number) const date = new Date(Date.UTC(year, month - 1, 1)) const timestamp = date.getTime() return timestamp } // 法将 YYYY-MM-DD、YYYY-MM 或 YYYY 格式的日期转换为时间戳 export const dateToTimestamp = (dateStr) => { if (!dateStr) return null if (dateStr.length === 4) { dateStr += '-01-01' // YYYY -> YYYY-01-01 } else if (dateStr.length === 7) { dateStr += '-01' // YYYY-MM -> YYYY-MM-01 } return new Date(dateStr).getTime() } export const getTimeDifferenceInChinese = (startTime, endTime) => { // 将时间戳转换为 Date 对象(假设时间戳单位为毫秒) const startDate = startTime ? new Date(startTime) : new Date(); const endDate = endTime ? new Date(endTime) : new Date(); // 计算年份差 let yearsDiff = endDate.getFullYear() - startDate.getFullYear(); // 计算月份差(考虑年份差后调整) let monthsDiff = endDate.getMonth() - startDate.getMonth(); // 如果月份差为负,则从上一年借月 if (monthsDiff < 0) { yearsDiff--; monthsDiff += 12; } // 计算日期差(考虑月份差后调整,如果日期差为负,则从上一月借天) let daysDiff = endDate.getDate() - startDate.getDate(); if (daysDiff < 0) { monthsDiff--; // 获取 startDate 所在月的最后一天 const lastDayOfMonth = new Date(startDate.getFullYear(), startDate.getMonth() + 1, 0).getDate(); daysDiff += lastDayOfMonth; // 加上最后一天以补全月份差 } // 构建结果字符串 let result = ""; if (yearsDiff > 0) { result += `${yearsDiff}年`; } if (monthsDiff > 0) { if (result) { // 如果已经有年份差异,则直接添加月份数(不带单位),后面正则替换会处理 result += monthsDiff; } else { // 如果没有年份差异,则正常添加月份和单位 result += `${monthsDiff}个月`; // 特别处理只有1个月的情况 if (monthsDiff === 1) { result = "不到1个月"; // 直接替换为“不到1个月”,避免后续复杂的正则替换 } } } else if (!result && daysDiff >= 0) { // 如果没有年份和月份差异,但天数差异存在(这里其实没处理天数,只是为完整性添加) // 理论上应该处理天数,但题目要求只到月份,所以这里直接返回“不到1个月” result = "不到1个月"; } // 如果之前添加了月份数但没有年份(且不是直接处理的1个月情况),则需要去除末尾多余的数字并添加“个月” if (result && !/\d年$/.test(result) && /\d$/.test(result)) { result += "个月"; } return result } // 设置面试邀请默认时间 export const getInterviewInviteDefaultTime = () => { const today = new Date() today.setDate(today.getDate() + 1) const time = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 10, 0, 0) return { time, timeStamp: time.getTime() } } export const getNextDate = (createTimestamp, format = 'YYYY-MM-DD', type, startTime) => { if (type === 'day') createTimestamp = createTimestamp * 24 * 60 * 60 * 1000 if (type === 'hour') createTimestamp = createTimestamp * 60 * 60 * 1000 let date = new Date(startTime ? startTime + createTimestamp : new Date().getTime() + createTimestamp) let year = date.getFullYear() let month = date.getMonth() + 1 let day = date.getDate() month = month < 10 ? `0${month}` : month day = day < 10 ? `0${day}` : day if (format === 'YYYY-MM') { return `${year}-${month}` } else if (format === 'YYYY-MM-DD') { return `${year}-${month}-${day}` } else { return `${year}-${month}-${day}` // 默认返回 yyyy-mm-dd } }