gitscope
← marzneshin / v0.2.0 / app/dashboard/src/utils/dateFormatter.ts
https://dev.kronoz.win/r/marzneshin.git
app/dashboard/src/utils/dateFormatter.ts · TypeScript
Blame Raw
 1import dayjs from 'dayjs';
 2
 3export const relativeExpiryDate = (expiryDate: number | null | undefined) => {
 4  let dateInfo = { status: '', time: '' };
 5  if (expiryDate) {
 6    if (
 7      dayjs(expiryDate * 1000)
 8        .utc()
 9        .isAfter(dayjs().utc())
10    ) {
11      dateInfo.status = 'expires';
12    } else {
13      dateInfo.status = 'expired';
14    }
15    const durationSlots: string[] = [];
16    const duration = dayjs.duration(
17      dayjs(expiryDate * 1000)
18        .utc()
19        .diff(dayjs())
20    );
21    if (duration.years() != 0) {
22      durationSlots.push(
23        Math.abs(duration.years()) +
24          ' year' +
25          (Math.abs(duration.years()) != 1 ? 's' : '')
26      );
27    }
28    if (duration.months() != 0) {
29      durationSlots.push(
30        Math.abs(duration.months()) +
31          ' month' +
32          (Math.abs(duration.months()) != 1 ? 's' : '')
33      );
34    }
35    if (duration.days() != 0) {
36      durationSlots.push(
37        Math.abs(duration.days()) +
38          ' day' +
39          (Math.abs(duration.days()) != 1 ? 's' : '')
40      );
41    }
42    if (durationSlots.length === 0) {
43      if (duration.hours() != 0) {
44        durationSlots.push(
45          Math.abs(duration.hours()) +
46            ' hour' +
47            (Math.abs(duration.hours()) != 1 ? 's' : '')
48        );
49      }
50      if (duration.minutes() != 0) {
51        durationSlots.push(
52          Math.abs(duration.minutes()) +
53            ' min' +
54            (Math.abs(duration.minutes()) != 1 ? 's' : '')
55        );
56      }
57    }
58    dateInfo.time = durationSlots.join(', ');
59  }
60  return dateInfo;
61};