| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- export class TimeFormatter {
- static nowDuration = 30
- //格式化时间
- static formatTimestamp(timestamp: number): string {
- const currentDate = new Date();
- const inputDate = new Date(timestamp);
- // 判断是否是今天
- if (
- inputDate.getDate() === currentDate.getDate() &&
- inputDate.getMonth() === currentDate.getMonth() &&
- inputDate.getFullYear() === currentDate.getFullYear()
- ) {
- return `今天 ${TimeFormatter.formatTime(inputDate)}`;
- }
- // 判断是否是昨天
- const yesterday = new Date();
- yesterday.setDate(currentDate.getDate() - 1);
- if (
- inputDate.getDate() === yesterday.getDate() &&
- inputDate.getMonth() === yesterday.getMonth() &&
- inputDate.getFullYear() === yesterday.getFullYear()
- ) {
- return `昨天 ${TimeFormatter.formatTime(inputDate)}`;
- }
- // 判断是否是明天
- const tomorrow = new Date();
- tomorrow.setDate(currentDate.getDate() + 1);
- if (
- inputDate.getDate() === tomorrow.getDate() &&
- inputDate.getMonth() === tomorrow.getMonth() &&
- inputDate.getFullYear() === tomorrow.getFullYear()
- ) {
- return `明天 ${TimeFormatter.formatTime(inputDate)}`;
- }
- // 返回 YYYY-MM-DD HH:mm
- return `${inputDate.getFullYear()}-${TimeFormatter.formatNumber(inputDate.getMonth() + 1)}-${TimeFormatter.formatNumber(
- inputDate.getDate()
- )} ${TimeFormatter.formatTime(inputDate)}`;
- }
- static dateTimeFormate(timestamp: number) {
- const currentDate = new Date();
- const inputDate = new Date(timestamp);
- // 判断是否是今天
- if (
- inputDate.getDate() === currentDate.getDate() &&
- inputDate.getMonth() === currentDate.getMonth() &&
- inputDate.getFullYear() === currentDate.getFullYear()
- ) {
- return `今天 ${TimeFormatter.padZero(inputDate.getHours())}:${TimeFormatter.padZero(inputDate.getMinutes())}`;
- }
- // 判断是否是昨天
- const yesterday = new Date();
- yesterday.setDate(currentDate.getDate() - 1);
- if (
- inputDate.getDate() === yesterday.getDate() &&
- inputDate.getMonth() === yesterday.getMonth() &&
- inputDate.getFullYear() === yesterday.getFullYear()
- ) {
- return `昨天 ${TimeFormatter.padZero(inputDate.getHours())}:${TimeFormatter.padZero(inputDate.getMinutes())}`;
- }
- return TimeFormatter.getDateAndWeek(timestamp)+' '+`${TimeFormatter.padZero(inputDate.getHours())}:${TimeFormatter.padZero(inputDate.getMinutes())}`
- }
- //1.只显示时间
- static timelineFormatTime(timestamp: number, isTestUser?: boolean): string {
- if (!timestamp) {
- return ' '
- }
- var date = new Date(timestamp)
- // 返回 HH:mm
- var str = `${TimeFormatter.formatNumber(date.getHours())}:${TimeFormatter.formatNumber(date.getMinutes())}`;
- if (isTestUser) {
- str += `:${TimeFormatter.formatNumber(date.getSeconds())}`
- }
- return str
- }
- //2.基于日期显示日期,省略今天描述
- static dateDescription(timestamp: number, showToday?: boolean): string {
- if (!timestamp) {
- return ''
- }
- const currentDate = new Date();
- const inputDate = new Date(timestamp);
- // 判断是否是今天
- if (
- inputDate.getDate() === currentDate.getDate() &&
- inputDate.getMonth() === currentDate.getMonth() &&
- inputDate.getFullYear() === currentDate.getFullYear()
- ) {
- if (currentDate.getTime() - timestamp <= TimeFormatter.nowDuration * 1000 && currentDate.getTime() - timestamp >= 0) {
- return '刚刚'
- }
- return showToday ? '今天' : ``;
- }
- // 判断是否是昨天
- const yesterday = new Date();
- yesterday.setDate(currentDate.getDate() - 1);
- if (
- inputDate.getDate() === yesterday.getDate() &&
- inputDate.getMonth() === yesterday.getMonth() &&
- inputDate.getFullYear() === yesterday.getFullYear()
- ) {
- return `昨天`;
- }
- // 判断是否是明天
- const tomorrow = new Date();
- tomorrow.setDate(currentDate.getDate() + 1);
- if (
- inputDate.getDate() === tomorrow.getDate() &&
- inputDate.getMonth() === tomorrow.getMonth() &&
- inputDate.getFullYear() === tomorrow.getFullYear()
- ) {
- return `明天`;
- }
- const utcDate1 = Date.UTC(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate())
- const utcDate2 = Date.UTC(inputDate.getFullYear(), inputDate.getMonth(), inputDate.getDate())
- const oneDayMilliseconds = 24 * 60 * 60 * 1000; // 一天的毫秒数
- var dayDifference = Math.floor((utcDate2 - utcDate1) / oneDayMilliseconds);
- if (dayDifference < 0) {
- // dayDifference = 0 - dayDifference;
- // return `${dayDifference}天前`
- var strDt = ''
- if (currentDate.getFullYear() != inputDate.getFullYear()) {
- strDt = inputDate.getFullYear() + '年'
- }
- strDt = strDt + (inputDate.getMonth() + 1) + '月'
- strDt = strDt + (inputDate.getDate()) + '日'
- return strDt
- }
- return `${dayDifference}天后`
- // return `${inputDate.getFullYear()}-${TimeFormatter.formatNumber(inputDate.getMonth() + 1)}-${TimeFormatter.formatNumber(
- // inputDate.getDate()
- // )}`;
- }
- //3. datedescription+时间,刚刚时隐藏时间
- static datetimeDescription(timestamp: number): string {
- const currentDate = new Date();
- if (currentDate.getTime() - timestamp <= TimeFormatter.nowDuration * 1000) {
- return TimeFormatter.dateDescription(timestamp, true)
- }
- return TimeFormatter.dateDescription(timestamp, true) + '' + TimeFormatter.timeDescription(timestamp)
- }
- static timeDescription(timestamp): string {
- const date = new Date(timestamp);
- return `${TimeFormatter.padZero(date.getHours())}:${TimeFormatter.padZero(date.getMinutes())}`;
- }
- static formatTime(date: Date): string {
- return `${TimeFormatter.formatNumber(date.getHours())}:${TimeFormatter.formatNumber(date.getMinutes())}:${TimeFormatter.formatNumber(date.getSeconds())}`;
- }
- static formatNumber(num: number): string {
- return num.toString().padStart(2, '0');
- }
- static getYearByDate(num: number): string {
- return (num + '').substring(0, 4) + '年'
- }
- static getMonthAndDayByDate(num: number): string {
- const dt = new Date((num + '').substring(0, 4) + '/' +
- (num + '').substring(4, 6) + '/' +
- (num + '').substring(6));
- const now = new Date();
- const diff = now.getTime() - dt.getTime();
- const day = 1000 * 60 * 60 * 24;
- if (diff < day) {
- return '今天';
- } else if (diff < 2 * day) {
- return '昨天';
- } else {
- var month = parseInt((num + '').substring(4, 6))
- var date = parseInt((num + '').substring(6, 8))
- return month + '月' + date + '日'
- }
- }
- //duration 根据起终点时间,忽略精度,如果两个时间点小于60s,则保留精度,否则为分钟
- static durationFormate(startTimestamp: number, endTimestamp: number) {
- var start = startTimestamp;
- var end = endTimestamp;
- const diff = Math.abs(end - start);
- if (diff > 60000) {
- var startTime = new Date(start)
- startTime.setSeconds(0)
- startTime.setMilliseconds(0)
- var endTime = new Date(end)
- endTime.setSeconds(0)
- endTime.setMilliseconds(0)
- start = startTime.getTime()
- end = endTime.getTime()
- }
- return TimeFormatter.calculateTimeDifference(start, end)
- }
- //计算时间间隔
- static calculateTimeDifference(startTimestamp: number, endTimestamp: number, ingoreSeconds?: boolean): string {
- const diff = Math.abs(endTimestamp - startTimestamp);
- // 计算小时、分钟和秒数
- const hours = Math.floor(diff / (1000 * 60 * 60));
- const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
- const seconds = Math.floor((diff % (1000 * 60)) / 1000);
- // 根据间隔的大小返回不同的格式
- if (diff < 60000) {
- return `${seconds}秒`;
- } else if (diff < 3600000) {
- return `${minutes}分钟`;
- // return `${minutes}分${seconds}秒`;
- } else {
- // if (ingoreSeconds) return `${hours}小时${minutes}分钟`;
- // return `${hours}小时${minutes}分${seconds}秒`;
- if (minutes == 0) {
- return `${hours}小时`;
- }
- return `${hours}小时${minutes}分钟`;
- }
- }
- //格式化时间间隔
- static formateTimeDifference(startTimestamp: number, endTimestamp: number, ingoreSeconds?: boolean): string {
- const diff = Math.abs(endTimestamp - startTimestamp);
- // 计算小时、分钟和秒数
- const hours = Math.floor(diff / (1000 * 60 * 60));
- const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
- const seconds = Math.floor((diff % (1000 * 60)) / 1000);
- // 根据间隔的大小返回不同的格式
- if (diff < 60000) {
- return `00:00:${TimeFormatter.padZero(seconds)}`;
- } else if (diff < 3600000) {
- return `00:${TimeFormatter.padZero(minutes)}:${TimeFormatter.padZero(seconds)}`;
- } else {
- if (ingoreSeconds) return `${hours}小时${minutes}分`;
- return `${TimeFormatter.padZero(hours)}:${TimeFormatter.padZero(minutes)}:${TimeFormatter.padZero(seconds)}`;
- }
- }
- static formateTime(timestamp: number) {
- const date = new Date(timestamp);
- return `${TimeFormatter.padZero(date.getMonth() + 1)}-${TimeFormatter.padZero(date.getDate())} ${TimeFormatter.padZero(date.getHours())}:${TimeFormatter.padZero(date.getMinutes())}:${TimeFormatter.padZero(date.getSeconds())}`;
- }
- static formateHourMinute(startTimestamp: number, endTimestamp: number): string {
- const diff = Math.abs(endTimestamp - startTimestamp);
- // 计算小时、分钟和秒数
- const hours = Math.floor(diff / (1000 * 60 * 60));
- const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
- return (hours < 10 ? `0${hours}` : `${hours}`) + ':' + (minutes < 10 ? `0${minutes}` : `${minutes}`);
- }
- static countdown = (dt: number): string => {
- const end = Date.now();
- const time = end > dt ? Math.floor((end - dt) / 1000) : Math.ceil((dt - end) / 1000)//Math.ceil((end>dt?end-dt:dt-end)/1000);
- const hours = Math.floor(time / 3600);
- const minutes = Math.floor((time % 3600) / 60);
- const seconds = Math.floor(time % 60);
- return `${TimeFormatter.padZero(hours)}:${TimeFormatter.padZero(minutes)}:${TimeFormatter.padZero(seconds)}`;
- };
- //计算正计时
- static formateTimeNow = (dt: number): string => {
- const end = Date.now();
- const time = Math.floor((end > dt ? end - dt : dt - end) / 1000);
- const hours = Math.floor(time / 3600);
- const minutes = Math.floor((time % 3600) / 60);
- const seconds = Math.floor(time % 60);
- return `${TimeFormatter.padZero(hours)}:${TimeFormatter.padZero(minutes)}:${TimeFormatter.padZero(seconds)}`;
- };
- static padZero = (num: number): string => {
- return num.toString().padStart(2, '0');
- };
- //根据时间段和一个时间,推算另外一个时间
- static calculateTimeByTimeRange = (timeRange: number, strTime: string, isStart: boolean): string => {
- console.log(timeRange, strTime, isStart)
- var list = strTime.split(':');
- var time = parseInt(list[0]) * 60 + parseInt(list[1]);
- if (isStart) {
- time = time + timeRange;
- }
- else {
- time = time - timeRange;
- }
- time = time < 0 ? time + 24 * 60 : time;
- time = time >= 24 * 60 ? time - 24 * 60 : time;
- var hour = Math.floor(time / 60);
- var minute = time % 60;
- return `${TimeFormatter.padZero(hour)}:${TimeFormatter.padZero(minute)}`;
- }
- //获取今天的日期和星期几
- static getDateAndWeek = (timestamp: number, ignore = false) => {
- const now = new Date();
- var dt = new Date(timestamp)
- dt.setSeconds(0)
- dt.setMilliseconds(0)
- dt.setMinutes(0)
- dt.setHours(0)
- const diff = now.getTime() - dt.getTime();
- const day = 1000 * 60 * 60 * 24;
- if (diff < day && !ignore) {
- return '今天';
- } else if (diff < 2 * day && !ignore) {
- return '昨天';
- } else {
- var weeks = ['日', '一', '二', '三', '四', '五', '六']
- return `${dt.getMonth() + 1}月${dt.getDate()}日 星期${weeks[dt.getDay()]}`
- }
- }
- //获取当前时间(hh:mm)
- static getCurrentHourAndMinute = () => {
- var now = new Date()
- return `${TimeFormatter.padZero(now.getHours())}:${TimeFormatter.padZero(now.getMinutes())}`;
- }
- }
|