-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtl-event-data.ts
70 lines (61 loc) · 1.67 KB
/
tl-event-data.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
// interface class for dealing with date calculations/formats
import moment, { Moment } from "moment";
export interface TlEventData {
title: string;
startDate: [year: number, month: number, day: number];
endDate: [year: number, month: number, day: number];
}
export class TlEventHelper {
// get date values
static convertToDisplayDate(date: TlEventData["startDate"]) {
const [year, month, day] = date;
const result = moment(0);
if (year != null) {
result.year(year);
}
if (month != null) {
result.month(month - 1);
}
if (day != null) {
result.date(day);
}
return result;
}
// get date format
static convertToDisplayDateFormat(date: TlEventData["startDate"]) {
const [year, month, day] = date;
let format = "YYYY";
if (month != null) {
format = "MMMM YYYY";
}
if (day != null) {
format = "D. MMMM YYYY";
}
return format;
}
// get display date: date.format + add BCE & remove "-"
static displayDate(date: TlEventData["startDate"]) {
const yearBCE = this.checkForYearBC(date);
let displayDate;
if (yearBCE) {
displayDate = this.convertToDisplayDate(date).format(
this.convertToDisplayDateFormat(date)
);
return displayDate.replace("-", "") + " BCE";
} else {
displayDate = this.convertToDisplayDate(date).format(
this.convertToDisplayDateFormat(date)
);
return displayDate;
}
}
// check if year includes "-"
static checkForYearBC(date: TlEventData["startDate"]) {
const year = date[0];
const yearString = year.toString();
if (yearString.includes("-")) {
return true;
}
return false;
}
}