]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/core-utils/common/date.ts
Merge branch 'release/4.0.0' into develop
[github/Chocobozzz/PeerTube.git] / shared / core-utils / common / date.ts
CommitLineData
34c7f429
C
1function isToday (d: Date) {
2 const today = new Date()
3
4 return areDatesEqual(d, today)
5}
6
7function isYesterday (d: Date) {
8 const yesterday = new Date()
9 yesterday.setDate(yesterday.getDate() - 1)
10
11 return areDatesEqual(d, yesterday)
12}
13
14function isThisWeek (d: Date) {
15 const minDateOfThisWeek = new Date()
16 minDateOfThisWeek.setHours(0, 0, 0)
17
18 // getDay() -> Sunday - Saturday : 0 - 6
19 // We want to start our week on Monday
20 let dayOfWeek = minDateOfThisWeek.getDay() - 1
21 if (dayOfWeek < 0) dayOfWeek = 6 // Sunday
22
23 minDateOfThisWeek.setDate(minDateOfThisWeek.getDate() - dayOfWeek)
24
25 return d >= minDateOfThisWeek
26}
27
28function isThisMonth (d: Date) {
29 const thisMonth = new Date().getMonth()
30
31 return d.getMonth() === thisMonth
32}
33
93aa8552
C
34function isLastMonth (d: Date) {
35 const now = new Date()
36
37 return getDaysDifferences(now, d) <= 30
38}
39
40function isLastWeek (d: Date) {
41 const now = new Date()
42
43 return getDaysDifferences(now, d) <= 7
44}
45
15a7eafb
C
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
34c7f429
C
89// ---------------------------------------------------------------------------
90
91export {
92 isYesterday,
93 isThisWeek,
94 isThisMonth,
93aa8552
C
95 isToday,
96 isLastMonth,
15a7eafb
C
97 isLastWeek,
98 timeToInt,
99 secondsToTime
34c7f429
C
100}
101
102// ---------------------------------------------------------------------------
103
104function areDatesEqual (d1: Date, d2: Date) {
105 return d1.getFullYear() === d2.getFullYear() &&
106 d1.getMonth() === d2.getMonth() &&
107 d1.getDate() === d2.getDate()
108}
93aa8552
C
109
110function getDaysDifferences (d1: Date, d2: Date) {
111 return (d1.getTime() - d2.getTime()) / (86400000)
112}