方法里可能有其他的逻辑,我没有删去多余的 ,仅供参考
1、对日期进行格式化
/*** 对日期进行格式化, 和C#大致一致 默认yyyy-MM-dd HH:mm:ss* 可不带参数 一个日期参数 或一个格式化参数* @param date 要格式化的日期* @param format 进行格式化的模式字符串* 支持的模式字母有:* y:年,* M:年中的月份(1-12),* d:月份中的天(1-31),* H:小时(0-23),* h:小时(0-11),* m:分(0-59),* s:秒(0-59),* f:毫秒(0-999),* q:季度(1-4)* @return String* @author adswads@gmail.com*/
export function dateFormat(date?: any, format?: string): string {/*** 返回字符串 为n个char构成* @param char 重复的字符* @param count 次数* @return String* @author adswads@gmail.com*/function charString(char: string, count: number): string {let str = ''while (count--) {str += char}return str}// 毫秒时间转为Dateif (date != undefined && typeof date == 'number') {date = new Date(date)}//无参数if (date == undefined && format == undefined) {date = new Date()format = 'yyyy-MM-dd HH:mm:ss'}//无日期else if (typeof date == 'string') {format = datedate = new Date()}//无格式化参数else if (format === undefined) {format = 'yyyy-MM-dd HH:mm:ss'} else {}//没有分隔符的特殊处理const map = {y: date.getFullYear() + '', //年份M: date.getMonth() + 1 + '', //月份d: date.getDate() + '', //日H: date.getHours(), //小时 24m: date.getMinutes() + '', //分s: date.getSeconds() + '', //秒q: Math.floor((date.getMonth() + 3) / 3) + '', //季度f: date.getMilliseconds() + '' //毫秒}//小时 12if (map['H'] > 12) {map['h'] = map['H'] - 12 + ''} else {map['h'] = map['H'] + ''}map['H'] += ''const reg = 'yMdHhmsqf'let all = '',str = ''for (let i = 0, n = 0; i < reg.length; i++) {n = format.indexOf(reg[i])if (n < 0) {continue}all = ''for (; n < format.length; n++) {if (format[n] != reg[i]) {break}all += reg[i]}if (all.length > 0) {if (all.length == map[reg[i]].length) {str = map[reg[i]]} else if (all.length > map[reg[i]].length) {if (reg[i] == 'f') {str = map[reg[i]] + charString('0', all.length - map[reg[i]].length)} else {str = charString('0', all.length - map[reg[i]].length) + map[reg[i]]}} else {switch (reg[i]) {case 'y':str = map[reg[i]].substr(map[reg[i]].length - all.length)breakcase 'f':str = map[reg[i]].substr(0, all.length)breakdefault:str = map[reg[i]]break}}format = format.replace(all, str)}}return format
}
2、格式化时间戳(Jun.02 08:54)
const MONTH_ABBR =['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']
export function formatEnDate(value: number) {if (!value) {return ''}const date: Date = new Date(value)const d = {Y: date.getFullYear(),M: MONTH_ABBR[date.getMonth()],D: date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),h: date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),m: date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()}return d.M + '.' + d.D + ' ' + d.h + ':' + d.m
}
3、格式化时间戳(YYYY/MM/DD hh:mm:ss)
/*** 格式化时间戳* 输入结果如 YYYY/MM/DD hh:mm:ss* @param milliseconds*/
export function formatMonthYearDate(value: number) {if (!value) {return ''}const date: Date = new Date(value)const d = {Y: date.getFullYear(),M: date.getMonth(),D: date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),h: date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),m: date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),s: date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()}return d.Y + '/' + d.M + '/' + d.D + ' ' + d.h + ':' + d.m + ':' + d.s
}
export const formatDate = (time: number) => {const date = new Date(time)const year = date.getFullYear()const month = date.getMonth() + 1const day = date.getDate()const hour = date.getHours()const minute = date.getMinutes()const second = date.getSeconds()return (year +'/' +month.toString().padStart(2, '0') +'/' +day.toString().padStart(2, '0') +' ' +hour.toString().padStart(2, '0') +':' +minute.toString().padStart(2, '0') +':' +second.toString().padStart(2, '0'))
}
4、格式化时间戳(hh:mm:ss)
export function getTimeStr(timestamp: number): string {if (timestamp) {const date = new Date(timestamp)const hours = ('' + date.getHours()).padStart(2, '0')const minutes = ('' + date.getMinutes()).padStart(2, '0')const seconds = ('' + date.getSeconds()).padStart(2, '0')return `${hours}:${minutes}:${seconds}`} else {return ''}
}
5、毫秒数 转 HH:MM:SS
export function timeToString(time: number): string {const t = time / 1000const hour = parseInt('' + t / 3600)const min = parseInt('' + (t % 3600) / 60)const se = parseInt('' + ((t % 3600) % 60))const hh = hour.toString().padStart(2, '0')const mm = min.toString().padStart(2, '0')const ss = se.toString().padStart(2, '0')return `${hh}:${mm}:${ss}`
}
6、当前时间与输入时间之间的差值
/*** 计算当前时间与输入时间之间的差值* @param milliseconds*/
export function formatCountdownRule(sendTime: number) {// 当前时间// const sendTime = new Date('2023.06.01 15:00:00').getTime()const timeDiff = sendTime - new Date().getTime() //时间差的毫秒数const isNegative = timeDiff < 0 ? true : false// 计算出总秒数和剩余毫秒数const totalSeconds = Math.floor(Math.abs(timeDiff) / 1000)// 计算出总分钟数和剩余秒数const totalMinutes = Math.floor(totalSeconds / 60)// const remainingSeconds = totalSeconds % 60// 计算出总小时数和剩余分钟数const totalHours = Math.floor(totalMinutes / 60)const remainingMinutes = totalMinutes % 60// 计算出总天数和剩余小时数const totalDays = Math.floor(totalHours / 24)const remainingHours = totalHours % 24if (isNegative) {// 剩余时间为负数 显示红色if (totalDays > 0) {return { time: '-' + totalDays + 'd', bgColor: 'bgRed' }}if (remainingHours > 0) {return { time: '-' + remainingHours + 'h', bgColor: 'bgRed' }}if (remainingMinutes >= 0) {return { time: '-' + remainingMinutes + 'm', bgColor: 'bgRed' }}} else {//剩余时间为 d 或者 h 绿色if (totalDays > 0) {return { time: totalDays + 'd', bgColor: 'bgGreen' }}if (remainingHours > 0) {return { time: remainingHours + 'h', bgColor: 'bgGreen' }}//剩余时间为 m 橙色if (remainingMinutes >= 0) {return { time: remainingMinutes + 'm', bgColor: 'bgOrange' }}}
}