-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrust-calendar.js
60 lines (56 loc) · 1.67 KB
/
rust-calendar.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
const moment = require('moment');
function CalculateCurrentWeekOfTheMonth() {
const currentTime = moment();
const year = currentTime.year();
const monthNumberZeroIndexed = currentTime.month();
let d = moment([year, monthNumberZeroIndexed, 1, 18, 0, 0]);
let thursdayCount = 0;
while (d.isBefore(currentTime)) {
const dayOfWeek = d.day();
const thursday = 4;
if (dayOfWeek === thursday) {
thursdayCount++;
}
d.add(24, 'hours');
}
return thursdayCount;
}
function CalculateHowManyThursdaysThisMonth() {
const currentTime = moment();
const year = currentTime.year();
const monthNumberZeroIndexed = currentTime.month();
let d = moment([year, monthNumberZeroIndexed, 1, 18, 0, 0]);
let thursdayCount = 0;
while (d.month() === monthNumberZeroIndexed) {
const dayOfWeek = d.day();
const thursday = 4;
if (dayOfWeek === thursday) {
thursdayCount++;
}
d.add(24, 'hours');
}
return thursdayCount;
}
// Gives the timestamp of every Thursday this month at 18:00 UTC.
function CalculateArrayOfAllThursdayEpochsThisMonth() {
const currentTime = moment();
const year = currentTime.year();
const monthNumberZeroIndexed = currentTime.month();
let d = moment([year, monthNumberZeroIndexed, 1, 18, 0, 0]);
const thursdays = [];
while (d.month() === monthNumberZeroIndexed) {
const dayOfWeek = d.day();
const thursday = 4;
if (dayOfWeek === thursday) {
const epoch = d.unix();
thursdays.push(epoch);
}
d.add(24, 'hours');
}
return thursdays;
}
module.exports = {
CalculateArrayOfAllThursdayEpochsThisMonth,
CalculateCurrentWeekOfTheMonth,
CalculateHowManyThursdaysThisMonth,
};