import moment from 'moment' /* eslint-disable @typescript-eslint/no-explicit-any */ /* 参考博客 https://blog.csdn.net/qq_36228377/article/details/125196997 */ export function formatTime(curTime: string, formatVal: string) { /* 格式化时间 curTIme:要格式化的时间 formatVal:格式化时间的格式 */ // 例如 curTime 2021-02-03 12-21-22 formatVal YYYY年MM月DD日 HH时mm分ss秒 return moment(curTime).format(formatVal) } // 时间格式 /* M 数字表示的月份,没有前导零 1到12 MM 数字表示的月份,有前导零 01到12 MMM 三个字母缩写表示的月份 Jan到Dec MMMM 月份,完整的文本格式 January到December Q 季度 1到4 D 月份中的第几天,没有前导零 1到31 DD 月份中的第几天,有前导零 01到31 d 星期中的第几天,数字表示 0到6,0表示周日,6表示周六 ddd 三个字母表示星期中的第几天 Sun到Sat dddd 星期几,完整的星期文本 从Sunday到Saturday w 年份中的第几周 如42:表示第42周 YYYY 四位数字完整表示的年份 如:2014 或 2000 YY 两位数字表示的年份 如:14 或 98 A 大写的AM PM AM PM a 小写的am pm am pm HH 小时,24小时制,有前导零 00到23 H 小时,24小时制,无前导零 0到23 hh 小时,12小时制,有前导零 00到12 h 小时,12小时制,无前导零 0到12 m 没有前导零的分钟数 0到59 mm 有前导零的分钟数 00到59 s 没有前导零的秒数 1到59 ss 有前导零的描述 01到59 X Unix时间戳 1411572969 */ export function getDiffTime(start: string, end: string, unit: any) { /* start 开始时间 end 结束时间 unit 时间单位 */ moment(start).diff(moment(end), unit) //32 } /** * 格式化日期字符串 * timestamp 时间戳 */ export function formatDate(timestamp: any) { // 补全为13位 const arrTimestamp: any = (timestamp + '').split('') for (let start = 0; start < 13; start++) { if (!arrTimestamp[start]) { arrTimestamp[start] = '0' } } timestamp = arrTimestamp.join('') * 1 const minute = 1000 * 60 const hour = minute * 60 const day = hour * 24 const month = day * 30 const now = new Date().getTime() const diffValue = now - timestamp // 如果本地时间反而小于变量时间 if (diffValue < 0) { return '不久前' } // 计算差异时间的量级 const monthC: any = diffValue / month const weekC: any = diffValue / (7 * day) const dayC: any = diffValue / day const hourC: any = diffValue / hour const minC: any = diffValue / minute // 数值补0方法 const zero = function (value: number) { if (value < 10) { return '0' + value } return value } // 使用 if (monthC > 4) { // 超过1年,直接显示年月日 return (function () { const date = new Date(timestamp) return ( date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日' ) })() } else if (monthC >= 1) { return parseInt(monthC) + '月前' } else if (weekC >= 1) { return parseInt(weekC) + '周前' } else if (dayC >= 1) { return parseInt(dayC) + '天前' } else if (hourC >= 1) { return parseInt(hourC) + '小时前' } else if (minC >= 1) { return parseInt(minC) + '分钟前' } return '刚刚' } export const getNewDay = (dateTemp: any, days: any,separator:any = '.') => { console.log("dateTemp",dateTemp,days) if(!dateTemp ){ return "" } dateTemp = dateTemp.split(separator); //转换为MM-DD-YYYY格式 let nDate: any = new Date(dateTemp[1] + "-" + dateTemp[2] + "-" + dateTemp[0]); let millSeconds: any = Math.abs(nDate) + days * 24 * 60 * 60 * 1000; let rDate: any = new Date(millSeconds); let year: any = rDate.getFullYear(); let month: any = rDate.getMonth() + 1; if (month < 10) month = "0" + month; let date = rDate.getDate(); if (date < 10) date = "0" + date; return year + "." + month + "." + date; }