-
Notifications
You must be signed in to change notification settings - Fork 5
/
date-helpers.js
158 lines (137 loc) · 4.17 KB
/
date-helpers.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* loosely based on the CLX framework */
import parseISO from 'date-fns/parseISO';
import format from 'date-fns/format';
import de from 'date-fns/locale/de';
import fr from 'date-fns/locale/fr';
import { getLanguage } from './translate';
// longDateFormats from moment.js
const formats = {
'de-CH': {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
L: 'dd.MM.yyyy',
LL: 'EEEEEE, d. MMMM yyyy',
LLL: 'EEEEEE, d. MMMM yyyy HH:mm',
LLLL: 'EEEE, d. MMMM yyyy HH:mm'
},
'fr-CH': {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
L: 'dd.MM.yyyy',
LL: 'EEEEEE, d MMMM yyyy',
LLL: 'EEEEEE, d MMMM yyyy HH:mm',
LLLL: 'EEEE d MMMM yyyy HH:mm'
}
};
const language = getLanguage();
const locale = language === 'de-CH' ? de : fr;
/**
* format a date
* @param {date|string|number|null} date the date to format
* @param {string} formatString format or longDateFormat from moment.js
*/
export function formatDate(date, formatString = '') {
if (date === null)
return null;
if (typeof date === 'string')
date = parseISO(date);
formatString = formatString in formats[language] ?
formats[language][formatString] : formatString;
return format(date, formatString, { locale });
}
/**
* returns true when the current Date is between
* SubscriptionDateFrom/SubscriptionTimeFrom and
* SubscriptionDateTo/SubscriptionTimeTo
* @param {object} event the event to check
*/
export function isInSubscriptionRange(event) {
let now = new Date();
if (event.SubscriptionFrom === null)
return now.getTime() < event.SubscriptionTo.getTime();
return event.SubscriptionFrom.getTime() < now.getTime() &&
now.getTime() < event.SubscriptionTo.getTime();
}
/**
* return true if DateFrom or SubscriptionDateTo is greater than or equal
* to the current date
* @param {object} event event to check
*/
export function eventStarted(event) {
let now = new Date();
if (event.DateFrom === null) {
return true;
}
let date = event.DateFrom <= event.SubscriptionDateTo ? event.SubscriptionDateTo : event.DateFrom;
return parseISO(date).getTime() >= now.getTime();
}
/**
* return true if DateTo is smaller than or equal
* to the current date
* @param {object} event event to check
*/
export function eventEnded(event) {
let now = new Date();
if (event.DateTo === null) {
return true;
}
return parseISO(event.DateTo).getTime() <= now.getTime();
}
/**
* combine a date and a time to a single date object
* this returns null when it fails
* @param {string} dateString a string containing the date
* @param {string} timeString a string containing the time in the format hh:mm
*/
export function combineDate(dateString, timeString) {
try {
let [hours, minutes] = timeString.split(':').map(str => parseInt(str));
let date = parseISO(dateString);
date.setHours(hours, minutes);
return date;
} catch (exception) {
return null;
}
}
/**
* return timeString in format 00:00 if it has the format hh:mm:ss
* @param {string} timeString the string to remove the time from
*/
export function removeMinutes(timeString) {
return timeString.replace(/^(\d\d:\d\d):\d\d$/g, '$1');
}
/**
* returns true if the format is DD.MM.YYYY
* @param {string} dateString the date to check
*/
function isDMY(dateString) {
return /^[0-9]{2}.[0-9]{2}.[0-9]{4}$/.test(dateString);
}
/**
* returns dateString in the format DD.MM.YYYY
* @param {string} dateString the date to convert
*/
export function getDMY(dateString) {
return isDMY(dateString) ? dateString : formatDate(dateString, 'L');
}
/**
* returns dateString in the format YYYY-MM-DD
* @param {string} dateString the date to convert
*/
export function getYMD(dateString) {
return isDMY(dateString) ? dateString.split('.').reverse().join('-') : formatDate(dateString, 'yyyy-MM-dd');
}
/**
* returns dateString in from format yyyy-mm-ddThh:mm:ss to yyyy\mm\dd hh:mm:ss
* @param {string} dateString the date to convert
*/
export function getDateTimeForIcs(dateString) {
return dateString.replace(new RegExp('-', 'g'), '/').replace(new RegExp('T', 'g'), ' ');
}
/**
* returns true if date > now
* @param {string} dateString YYYY-MM-DD
*/
export function dateGreaterNow(date){
return parseISO(date) > Date.now() ? true : false;
}