summaryrefslogtreecommitdiffhomepage
path: root/gui/src/shared/account-expiry.ts
blob: 1b40848220894d17e78474447cb4acae9b97d51d (plain)
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
import {
  dateByAddingComponent,
  DateComponent,
  DateType,
  FormatDateOptions,
  formatRelativeDate,
} from './date-helper';

export function hasExpired(expiry: DateType): boolean {
  return new Date(expiry).getTime() < Date.now();
}

export function closeToExpiry(expiry: DateType, days = 3): boolean {
  return (
    !hasExpired(expiry) &&
    new Date(expiry) <= dateByAddingComponent(new Date(), DateComponent.day, days)
  );
}

export function formatDate(date: DateType, locale: string): string {
  return new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short' }).format(
    new Date(date),
  );
}

export function formatRemainingTime(expiry: DateType, options?: FormatDateOptions): string {
  return formatRelativeDate(new Date(), expiry, options);
}