forked from marcomerens/anniversarymodule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Birthdays.js
72 lines (61 loc) · 1.6 KB
/
MMM-Birthdays.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
/* Magic Mirror
* Module: MMM-Birthdays
*
* By Jean-Marie Straetemans
* derived from BIRTHDAYS
* Initially By Marco Merens
* Module: BIRTHDAYS
* MIT Licensed.
*/
Module.register("MMM-Birthdays", {
defaults: {
limit:4,
people:[{name:"Marco",birthdate:"1973-12-30"}],
maxDays: 30
},
start:function() {
this.calculate()
var self=this
setInterval(function(){
self.calculate()
self.updateDom()
},60*60*1000) // update every hour
},
calculate : function(){
var now=new Date(new Date().toLocaleString("sv").substring(0,10))
var thisyear=now.getUTCFullYear()
var today=new Date(thisyear,now.getUTCMonth(),now.getUTCDate()).getTime()
Log.log(now)
var months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
this.config.people.forEach(function(p) {
var bdate = new Date(p.birthdate)
var bmonth = bdate.getUTCMonth()
var bday = bdate.getUTCDate()
var byear = bdate.getUTCFullYear()
p.age = thisyear-byear
var diff = new Date(thisyear, bmonth, bday).getTime() - today
if (diff<0) {
diff = new Date(thisyear + 1,bmonth,bday).getTime()-today
p.age = thisyear - byear
}
p.days = Math.floor(diff/1000/60/60/24)
p.date = bday+" "+months[bmonth]
p.birthyear = byear
p.day = bday
})
this.config.people = this.config.people
.filter(function(p) {
return p.days <= this.config.maxDays;
}
.bind(this)) // Filter based on maxDays
.sort(function(a, b) {
return a.days - b.days;
});
},
getTemplate: function () {
return "MMM-Birthdays.njk";
},
getTemplateData: function () {
return this.config;
}
});