aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/common/date.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/core-utils/common/date.ts')
-rw-r--r--shared/core-utils/common/date.ts47
1 files changed, 46 insertions, 1 deletions
diff --git a/shared/core-utils/common/date.ts b/shared/core-utils/common/date.ts
index 4f92f758f..3e4a3c08c 100644
--- a/shared/core-utils/common/date.ts
+++ b/shared/core-utils/common/date.ts
@@ -43,6 +43,49 @@ function isLastWeek (d: Date) {
43 return getDaysDifferences(now, d) <= 7 43 return getDaysDifferences(now, d) <= 7
44} 44}
45 45
46function timeToInt (time: number | string) {
47 if (!time) return 0
48 if (typeof time === 'number') return time
49
50 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
51 const matches = time.match(reg)
52
53 if (!matches) return 0
54
55 const hours = parseInt(matches[2] || '0', 10)
56 const minutes = parseInt(matches[4] || '0', 10)
57 const seconds = parseInt(matches[6] || '0', 10)
58
59 return hours * 3600 + minutes * 60 + seconds
60}
61
62function secondsToTime (seconds: number, full = false, symbol?: string) {
63 let time = ''
64
65 if (seconds === 0 && !full) return '0s'
66
67 const hourSymbol = (symbol || 'h')
68 const minuteSymbol = (symbol || 'm')
69 const secondsSymbol = full ? '' : 's'
70
71 const hours = Math.floor(seconds / 3600)
72 if (hours >= 1) time = hours + hourSymbol
73 else if (full) time = '0' + hourSymbol
74
75 seconds %= 3600
76 const minutes = Math.floor(seconds / 60)
77 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
78 else if (minutes >= 1) time += minutes + minuteSymbol
79 else if (full) time += '00' + minuteSymbol
80
81 seconds %= 60
82 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
83 else if (seconds >= 1) time += seconds + secondsSymbol
84 else if (full) time += '00'
85
86 return time
87}
88
46// --------------------------------------------------------------------------- 89// ---------------------------------------------------------------------------
47 90
48export { 91export {
@@ -51,7 +94,9 @@ export {
51 isThisMonth, 94 isThisMonth,
52 isToday, 95 isToday,
53 isLastMonth, 96 isLastMonth,
54 isLastWeek 97 isLastWeek,
98 timeToInt,
99 secondsToTime
55} 100}
56 101
57// --------------------------------------------------------------------------- 102// ---------------------------------------------------------------------------