-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatDate.ts
40 lines (31 loc) · 1.06 KB
/
formatDate.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
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import localizedFormat from "dayjs/plugin/localizedFormat";
import "dayjs/locale/en-gb";
dayjs.locale("en-gb");
dayjs.extend(relativeTime);
dayjs.extend(localizedFormat);
/**
* Formats date as relative time.
* @param date Date to format.
* @param [currentDate] Current date to compared against (for testing).
* @returns Formatted date as relative time (up to two weeks).
*/
const formatDateRelative = (date: string, currentDate?: string): string => {
let formattedDate;
const targetDate = dayjs(date);
const now = dayjs(currentDate ?? undefined);
if (now.diff(targetDate, "week") >= 2) {
formattedDate = targetDate.format("DD/MM/YYYY");
} else {
formattedDate = targetDate.from(now);
}
return formattedDate;
};
/**
* Formats date as month name and year.
* @param date Date to format.
* @returns Formatted date as long format.
*/
const formatDateMonth = (date: string): string => dayjs(date).format("MMMM YYYY");
export { formatDateRelative, formatDateMonth };