-
Notifications
You must be signed in to change notification settings - Fork 1
/
DateSemaines.js
61 lines (56 loc) · 1.35 KB
/
DateSemaines.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
class DateSemaines {
static nextPage(weekObj) {
return this.changeWeek(weekObj, weekObj.perPage);
}
static previousPage(weekObj) {
return this.changeWeek(weekObj, weekObj.perPage * -1);
}
static getNew(perPage) {
return {
year: Number(new Date().toISOString().substring(0, 4)),
week: this.currentWeekNumber(),
perPage: perPage,
};
}
static changeWeek(weekObj, offset) {
var tmpYear = weekObj.year;
var tmpWeek = weekObj.week;
var weekSum = tmpWeek + offset;
if (weekSum > 52) {
tmpWeek = weekSum - 52;
} else if (weekSum < 0) {
tmpWeek = 52 + offset;
} else if (weekSum == 0) {
tmpWeek = 52;
} else if (weekSum > 0 && weekSum <= 52) {
tmpWeek = tmpWeek + offset;
}
if (!(weekSum > 0 && weekSum <= 52)) {
if (Math.sign(offset) == 1) {
tmpYear++;
} else {
tmpYear--;
}
}
return {
year: tmpYear,
week: tmpWeek,
perPage: weekObj.perPage,
};
}
static currentWeekNumber() {
let startDate = new Date();
let onejan = new Date(startDate.getFullYear(), 0, 1);
let week = Math.ceil(
((startDate.getTime() - onejan.getTime()) / 86400000 +
onejan.getDay() +
1) /
7
);
if (week > 52) {
week = week - 52;
}
return week;
}
}
module.exports = DateSemaines