blob: aa8a0bc3c987123fa97d13d70ed2c2dd9d884f26 (
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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
import {$gettext, $gettextInterpolate} from "./gettext";
class DateTime
{
/**
* A helper method for padding parts of dates with leading zeros.
* @param item The part of a date string to pad.
* @param target_length The length of the string to output.
* @returns {string} A padded version of $what.
*/
pad(item: number, target_length: number = 2) : string {
const target: string = `00000000${item}`;
return target.substring(target.length - target_length);
}
/**
* Returns an ISO representation of the specified Date object.
* in the format YYYY-MM-DD.
*
* @param date The Date object to format as ISO date.
* @returns {string} The ISO date string of the Date object.
*/
getISODate(date: Date) : string {
return date.getFullYear() + '-' + this.pad(date.getMonth() + 1) + '-' + this.pad(date.getDate());
}
/**
* Returns a formatted version of the specified Date object
* in the Stud.IP date formatting.
*
* @param date The Date object to be formatted.
* @param relative_value Whether to return a relative time value (true)
* or an absolute one (false). Defaults to false.
* @param date_only Whether to return the date only (true) or date and time (false).
* Defaults to false. Only regarded when $relative_value is false.
* @param html Whether to format the date as HTML (true) or as plain text (false). Defaults to false.
*
* @returns {*|string} The date, formatted according to the Stud.IP format for dates.
*/
getStudipDate(
date: Date,
relative_value: boolean = false,
date_only: boolean = false,
html: boolean = false) : string {
if (relative_value) {
const now: number = Date.now();
const date_ts: number = date.getMilliseconds();
if (now - date_ts < 60 * 1000) {
return $gettext('Jetzt');
}
if (now - date_ts < 2 * 60 * 60 * 1000) {
return $gettextInterpolate(
'Vor %{ minutes } Minuten',
{minutes: Math.floor((now - date_ts) / (1000 * 60))}
);
}
return this.pad(date.getHours()) + ':' + this.pad(date.getMinutes());
}
if (date_only) {
if (html) {
return '<span class="day">'
+ this.pad(date.getDate())
+ '.</span><span class="month">'
+ this.pad(date.getMonth() + 1)
+ '.</span><span class="year">'
+ date.getFullYear()
+ '</span>';
} else {
return this.pad(date.getDate()) + '.' + this.pad(date.getMonth() + 1) + '.' + date.getFullYear();
}
}
if (html) {
return '<span class="day">'
+ this.pad(date.getDate())
+ '.</span><span class="month">'
+ this.pad(date.getMonth() + 1)
+ '.</span><span class="year">'
+ date.getFullYear()
+ '</span> <span class="time">'
+ this.pad(date.getHours()) + ':' + this.pad(date.getMinutes())
+ '</span>';
} else {
return this.pad(date.getDate()) + '.' + this.pad(date.getMonth() + 1) + '.' + date.getFullYear() + ' ' + this.pad(date.getHours()) + ':' + this.pad(date.getMinutes());
}
}
getDayOfWeekName(dow: number, short: boolean = false): string {
if (dow === 0 || dow === 7) {
if (short) {
return $gettext('So.');
} else {
return $gettext('Sonntag');
}
} else if (dow === 1) {
if (short) {
return $gettext('Mo.');
} else {
return $gettext('Montag');
}
} else if (dow === 2) {
if (short) {
return $gettext('Di.');
} else {
return $gettext('Dienstag');
}
} else if (dow === 3) {
if (short) {
return $gettext('Mi.');
} else {
return $gettext('Mittwoch');
}
} else if (dow === 4) {
if (short) {
return $gettext('Do.');
} else {
return $gettext('Donnerstag');
}
} else if (dow === 5) {
if (short) {
return $gettext('Fr.');
} else {
return $gettext('Freitag');
}
} else if (dow === 6) {
if (short) {
return $gettext('Sa.');
} else {
return $gettext('Samstag');
}
} else {
return '';
}
}
}
export default DateTime;
export const datetime: DateTime = new DateTime();
|