diff --git a/src/time.ts b/src/time.ts index bfe701bd..968693ba 100644 --- a/src/time.ts +++ b/src/time.ts @@ -49,6 +49,11 @@ export function timeStampFormat( return fmt } +function beautiTime(time: number): string { + if (time >= 10) return `${time}` + return `0${time}` +} + /** * 将日期格式化 * @exports dateFormat @@ -201,22 +206,75 @@ export function getTimeAgoOrDate(time, format) { * getCountDownObj(1*24*60*60+200) // {day: 1, hour: 0, minute: 3, second: 20} * */ -export function getCountDownObj(time): object { +export function getCountDownObj( + time, + maxUnit = 'DAY', +): { + day?: Number + hour?: Number + minute?: Number + second?: Number + fDay?: String + fHour?: String + fMinute?: String + fSecond?: String +} { if (!time) { return {} } time = parseInt(time, 10) - const day = parseInt(`${time / (24 * 60 * 60)}`, 10) - const hour = parseInt(`${(time - day * 24 * 60 * 60) / (60 * 60)}`, 10) - const minute = parseInt( - `${(time - day * 24 * 60 * 60 - hour * 60 * 60) / 60}`, - 10, - ) - const second = time - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60 - return { - day, - hour, - minute, - second, + + const second = Math.floor(time % 60) + // 秒是最大单位 + if (maxUnit === 'SECOND') { + return { + second: time, + fSecond: beautiTime(second), + } + } + + let minute = Math.floor((time / 60) % 60) + // 分钟是最大单位 + if (maxUnit === 'MINUTE') { + minute = Math.floor(time / 60) + + return { + minute, + second, + fMinute: beautiTime(minute), + fSecond: beautiTime(second), + } + } + + let hour = Math.floor((time / 60 / 60) % 24) + // 小时为最大单位 + if (maxUnit === 'HOUR') { + hour = Math.floor(time / 60 / 60) + + return { + hour, + minute, + second, + fHour: beautiTime(hour), + fMinute: beautiTime(minute), + fSecond: beautiTime(second), + } + } + + // 天为最大单位 + if (maxUnit === 'DAY') { + const day = Math.floor(time / 60 / 60 / 24) + + return { + day, + hour, + minute, + second, + fDay: beautiTime(day), + fHour: beautiTime(hour), + fMinute: beautiTime(minute), + fSecond: beautiTime(second), + } } + return {} } diff --git a/test/time.test.ts b/test/time.test.ts index 2562b553..814795e2 100644 --- a/test/time.test.ts +++ b/test/time.test.ts @@ -1,9 +1,77 @@ -import { timeStampFormat } from '../src' +import { timeStampFormat, getCountDownObj } from '../src' -describe('', () => { +describe('timeStampFormat', () => { it('', () => { expect(timeStampFormat(1647503559488, 'yyyy-MM-dd hh:mm:ss')).toBe( '2022-03-17 15:52:39', ) }) }) + +describe('getCountDownObj', () => { + it('', () => { + expect(getCountDownObj(100)).toEqual({ + fDay: '00', + fHour: '00', + fMinute: '01', + fSecond: '40', + day: 0, + hour: 0, + minute: 1, + second: 40, + }) + + expect(getCountDownObj(1 * 24 * 60 * 60 + 200)).toEqual({ + fDay: '01', + fHour: '00', + fMinute: '03', + fSecond: '20', + day: 1, + hour: 0, + minute: 3, + second: 20, + }) + + expect( + getCountDownObj(1 * 24 * 60 * 60 + 2 * 60 * 60 + 1 * 60 + 11), + ).toEqual({ + fDay: '01', + fHour: '02', + fMinute: '01', + fSecond: '11', + day: 1, + hour: 2, + minute: 1, + second: 11, + }) + }) + + it('test maxUnit', () => { + expect(getCountDownObj(2 * 60 * 60 + 6 * 60 + 12, 'MINUTE')).toEqual({ + fMinute: '126', + fSecond: '12', + minute: 126, + second: 12, + }) + + expect(getCountDownObj(2 * 60 * 60 + 6 * 60 + 12, 'HOUR')).toEqual({ + fHour: '02', + fMinute: '06', + fSecond: '12', + hour: 2, + minute: 6, + second: 12, + }) + + expect( + getCountDownObj(1 * 24 * 60 * 60 + 2 * 60 * 60 + 1 * 60 + 11, 'HOUR'), + ).toEqual({ + fHour: '26', + fMinute: '01', + fSecond: '11', + hour: 26, + minute: 1, + second: 11, + }) + }) +})