Skip to content

Commit

Permalink
feat: 倒计时工具
Browse files Browse the repository at this point in the history
  • Loading branch information
novlan1 committed Apr 13, 2022
1 parent 30207f2 commit 131cbef
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 15 deletions.
84 changes: 71 additions & 13 deletions src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export function timeStampFormat(
return fmt
}

function beautiTime(time: number): string {
if (time >= 10) return `${time}`
return `0${time}`
}

/**
* 将日期格式化
* @exports dateFormat
Expand Down Expand Up @@ -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 {}
}
72 changes: 70 additions & 2 deletions test/time.test.ts
Original file line number Diff line number Diff line change
@@ -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,
})
})
})

0 comments on commit 131cbef

Please sign in to comment.