diff options
author | Chocobozzz <me@florianbigard.com> | 2019-05-31 11:14:38 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-05-31 11:15:50 +0200 |
commit | 34c7f429e411fb911dfa56a816b638fd665717a4 (patch) | |
tree | af6b7658788a2304301a4793b7e9a293c8d2e1e6 /shared/core-utils | |
parent | e6b04e0e796c1b36d15fbe732e89ef4820dcabd9 (diff) | |
download | PeerTube-34c7f429e411fb911dfa56a816b638fd665717a4.tar.gz PeerTube-34c7f429e411fb911dfa56a816b638fd665717a4.tar.zst PeerTube-34c7f429e411fb911dfa56a816b638fd665717a4.zip |
Group videos on chronological order
Diffstat (limited to 'shared/core-utils')
-rw-r--r-- | shared/core-utils/miscs/date.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/shared/core-utils/miscs/date.ts b/shared/core-utils/miscs/date.ts new file mode 100644 index 000000000..7f0b4443b --- /dev/null +++ b/shared/core-utils/miscs/date.ts | |||
@@ -0,0 +1,49 @@ | |||
1 | function isToday (d: Date) { | ||
2 | const today = new Date() | ||
3 | |||
4 | return areDatesEqual(d, today) | ||
5 | } | ||
6 | |||
7 | function isYesterday (d: Date) { | ||
8 | const yesterday = new Date() | ||
9 | yesterday.setDate(yesterday.getDate() - 1) | ||
10 | |||
11 | return areDatesEqual(d, yesterday) | ||
12 | } | ||
13 | |||
14 | function 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 | |||
28 | function isThisMonth (d: Date) { | ||
29 | const thisMonth = new Date().getMonth() | ||
30 | |||
31 | return d.getMonth() === thisMonth | ||
32 | } | ||
33 | |||
34 | // --------------------------------------------------------------------------- | ||
35 | |||
36 | export { | ||
37 | isYesterday, | ||
38 | isThisWeek, | ||
39 | isThisMonth, | ||
40 | isToday | ||
41 | } | ||
42 | |||
43 | // --------------------------------------------------------------------------- | ||
44 | |||
45 | function areDatesEqual (d1: Date, d2: Date) { | ||
46 | return d1.getFullYear() === d2.getFullYear() && | ||
47 | d1.getMonth() === d2.getMonth() && | ||
48 | d1.getDate() === d2.getDate() | ||
49 | } | ||