import Vue from 'vue'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import timezone from 'dayjs/plugin/timezone'; import _ from 'lodash'; interface UIDayOption { tz: boolean; format: string; } export class Dayjs { constructor() { this.init(); } public dayjs = dayjs; private option: UIDayOption = { tz: false, format: 'MM/DD/YYYY HH:mm:ss', }; private msgNODate = '[DEBUG] Date is Undefined!'; public area = ''; init() { dayjs.extend(utc); dayjs.extend(timezone); this.area = dayjs.tz.guess(); } public getEndingTimeOfDay(date: any) { // 处理 date 为00:00:00; const start_date = this.getDate(date); return this.dayjs(start_date).add(1, 'd').subtract(1, 's').toISOString(); } public isBeforeToday(strDate: string | number) { return this.dayjs(strDate).isBefore(new Date(), 'd'); } public isSameBeforeToday(strDate: string | number) { return !this.dayjs(strDate).isAfter(new Date(), 'd'); } private createOption(opt?: { tz?: boolean; format?: string }): UIDayOption { const option = _.cloneDeep(this.option); if (!opt) return option; opt.tz !== undefined && (option.tz = opt.tz); opt.format !== undefined && (option.format = opt.format); return option; } translate(strDate: string | number | Date, option: UIDayOption) { const d = this.dayjs(strDate); if (option.tz) { d.tz(this.area); } return d.format(option.format); } public getDate(strDate: string | number | Date, format?: string) { if (!strDate) return ''; // this.msgNODate; const option = this.createOption({ format: format || 'MM/DD/YYYY' }); return this.translate(strDate, option); } public getDateGmt(strDate: string | number | Date) { return this.dayjs(strDate).format(); } public getDateTime(strDate: string | number | Date) { if (!strDate) return ''; // this.msgNODate; const option = this.createOption(); return this.translate(strDate, option); } public getISOString(strDate: string | Date | number) { return this.dayjs(strDate).format(); } // public showDate(strDate: string): string { // return this.getDate(strDate); // } // // public showDateTime(strDate: string): string { // return this.getDateTime(strDate); // } // table 时间差 映射对应 字符串; // 计算时间差, 取绝对值 // 映射key采用range mapStringOfTimeDifference(timeStart: string, timeEnd: string, arrMap: any[]) { const timeDifference = Math.abs( new Date(timeStart).getTime() - new Date(timeEnd).getTime() ); let text = ''; // arrMap.forEach((option: any) => { for (let i = 0; i < arrMap.length; i++) { const option = arrMap[i]; if (option.range === 'Infinity') { if (timeDifference < +Infinity) { text = option.value; } } if (timeDifference < +option.range) { text = option.value; break; } } // }); return text; } //一分钟内:just now // 一小时内:1-59 minutes ago // 一天内:1-23 hours ago // 一周内: 1-6 days ago // 一个月内:1-4 weeks ago // 一年内:1-11 months ago // 大于一年:n years ago public getTextOfTableLastTime(time: string, time2?: string | Date) { if (!time2) { time2 = new Date(); } const arrMap: any[] = []; arrMap.push({ range: `${1000 * 60}`, value: 'just now', }); arrMap.push({ range: `${1000 * 60 * 60}`, value: `${this.dayjs(time2).diff(time, 'm')} minutes ago`, }); arrMap.push({ range: `${1000 * 60 * 60 * 24}`, value: `${this.dayjs(time2).diff(time, 'h')} hours ago`, }); arrMap.push({ range: `${1000 * 60 * 60 * 24 * 7}`, value: `${this.dayjs(time2).diff(time, 'd')} days ago`, }); arrMap.push({ range: `${1000 * 60 * 60 * 24 * 31}`, value: `${this.dayjs(time2).diff(time, 'w')} weeks ago`, }); arrMap.push({ range: `${1000 * 60 * 60 * 24 * 365}`, value: `${this.dayjs(time2).diff(time, 'M')} months ago`, }); arrMap.push({ range: 'Infinity', value: `${this.dayjs(time2).diff(time, 'y')} years ago`, }); return this.mapStringOfTimeDifference(time, time2.toString(), arrMap); } public getTextOfTableCountdown( time: string, time2: string | Date = new Date() ) { return `${Math.abs(this.dayjs(time).diff(time2, 'd'))} days`; } } let instance: Dayjs; export const getInstance = () => instance; export const useDayjs = () => { if (instance) return instance; instance = new Dayjs(); return instance; }; export const DayjsPlugin = { install(V: typeof Vue) { const day = useDayjs(); V.prototype.$adbDayjs = day; }, };