forked from jalkire/whenisthenexttuesdaycall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (52 loc) · 1.59 KB
/
index.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
module.exports = async function (context, req) {
Date.prototype.addDays = function(days) {
const date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
function getNextDayOfWeek(date, dayOfWeek) {
var resultDate = new Date(date.getTime());
resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
return resultDate;
}
function ISO8601_week_no(dt) {
const tdt = new Date(dt.valueOf());
const dayn = (dt.getDay() + 6) % 7;
tdt.setDate(tdt.getDate() - dayn + 3);
const firstThursday = tdt.valueOf();
tdt.setMonth(0, 1);
if (tdt.getDay() !== 4) {
tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7);
}
return 1 + Math.ceil((firstThursday - tdt) / 604800000);
}
let target = new Date();
const today = new Date();
if (target.getDay != 2) {
target = getNextDayOfWeek(target, 2);
}
const weekNr = ISO8601_week_no(target);
if(weekNr % 2 == 0) target.addDays(7);
let nextcall;
if (today.toDateString() == target.toDateString()) {
nextcall = 'Today';
} else {
nextcall = target.toLocaleDateString();
}
const responseMessage = `
<!DOCTYPE html>
<html>
<head>
<title>${nextcall}</title>
</head>
<body>
<h1>The next Every Other Tuesday Call is ${nextcall} at 7:00 PT!</h1>
</body>
</html>`;
context.res = {
body: responseMessage,
headers: {
"Content-Type": "text/html"
}
};
}