This repository has been archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdate.ts
109 lines (84 loc) · 2.88 KB
/
date.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// ----- Imports ----- //
import { Option, some, none } from '@guardian/types/option';
// ----- Setup ----- //
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
// ----- Functions ----- //
function isToday(date: Date): boolean {
const today = new Date();
return (date.toDateString() === today.toDateString());
}
function isWithin24Hours(date: Date): boolean {
const today = new Date();
return (date.valueOf() > (today.valueOf() - (24 * 60 * 60 * 1000)));
}
function isWithinPastWeek(date: Date): boolean {
const daysAgo = new Date().valueOf() - (7 * 24 * 60 * 60 * 1000);
return date.valueOf() >= daysAgo;
}
function isWithinPastYear(date: Date): boolean {
const weeksAgo = new Date().valueOf() - (52 * 7 * 24 * 60 * 60 * 1000);
return date.valueOf() >= weeksAgo;
}
function isValidDate(date: Date): boolean {
if (Object.prototype.toString.call(date) !== '[object Date]') {
return false;
}
return !isNaN(date.getTime());
}
function makeRelativeDate(date: Date): string | null {
const then: Date = new Date(date);
const now: Date = new Date();
if (!isValidDate(then)) {
return null;
}
const delta: number = parseInt(`${(now.valueOf() - then.valueOf()) / 1000}`, 10);
if (delta < 0) {
return null;
} else if (delta < 55) {
return `${delta}s`;
} else if (delta < (55 * 60)) {
const minutesAgo = Math.round(delta / 60);
if (minutesAgo === 1) {
return 'Now';
} else {
return `${minutesAgo}m ago`;
}
} else if (isToday(then) || isWithin24Hours(then)) {
const hoursAgo = Math.round(delta / 3600);
return `${hoursAgo}h ago`;
} else if (isWithinPastWeek(then)) {
const daysAgo = Math.round(delta / 3600 / 24);
return `${daysAgo}d ago`;
} else if (isWithinPastYear(then)) {
const weeksAgo = Math.round(delta / 3600 / 24 / 7);
return `${weeksAgo}w ago`;
} else {
const yearsAgo = Math.round(delta / 3600 / 24 / 7 / 52);
return `${yearsAgo}y ago`;
}
}
const day = (date: Date): string =>
days[date.getUTCDay()];
const month = (date: Date): string =>
months[date.getUTCMonth()];
const padZero = (n: number): string =>
n < 10 ? `0${n}` : n.toString();
const time = (date: Date): string =>
`${padZero(date.getUTCHours())}.${padZero(date.getUTCMinutes())}`;
const format = (date: Date): string =>
`${day(date)} ${date.getUTCDate()} ${month(date)} ${date.getUTCFullYear()} ${time(date)} UTC`;
function fromString(date: string): Option<Date> {
try {
return some(new Date(date));
} catch(e) {
return none;
}
}
// ----- Exports ----- //
export {
makeRelativeDate,
format as formatDate,
isValidDate,
fromString,
}