-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdateTimeParser.js
More file actions
31 lines (26 loc) · 977 Bytes
/
Copy pathdateTimeParser.js
File metadata and controls
31 lines (26 loc) · 977 Bytes
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
const chrono = require('chrono-node');
function parseDateTime(text) {
const results = chrono.parse(text);
if (results.length > 0) {
const startDate = results[0].start.date();
let endDate;
if (results[0].end) {
endDate = results[0].end.date();
} else {
// If no end time is specified, assume the meeting is 1 hour long
endDate = new Date(startDate.getTime() + 60 * 60 * 1000);
}
// Ensure the dates are in the future
const now = new Date();
if (startDate < now) {
const tomorrow = new Date(now);
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(startDate.getHours(), startDate.getMinutes(), 0, 0);
endDate = new Date(tomorrow.getTime() + (endDate.getTime() - startDate.getTime()));
startDate.setTime(tomorrow.getTime());
}
return { startDateTime: startDate, endDateTime: endDate };
}
return null;
}
module.exports = { parseDateTime };