-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
119 lines (115 loc) · 3.18 KB
/
script.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var birthcalc = {
startDate:null,
now:null,
diff:null,
live:null,
input:{
year:null,
day:null,
month:null,
hour:null,
minute:null
},
cal:{
year:null,
day:null,
month:null,
hour:null,
minute:null
},
init:function(){
this.input.year = $("#year");
this.input.day = $("#day");
this.input.month = $("#month");
this.input.hour = $("#hour");
this.input.minute = $("#minute");
this.fillCalendar();
this.updateNow();
this.liveUpdate("on");
},
liveUpdate:function(action){
switch(action){
case "on":
this.live = setInterval(function(){
birthcalc.generateStats();
}, 1000);
break;
case "off":
clearInterval(this.live);
break;
}
},
updateNow:function(){
var tempdate = new Date();
//Get now normalized to 0 seconds.
this.now = new Date(
tempdate.getFullYear(),
tempdate.getMonth(),
tempdate.getDate(),
tempdate.getHours(),
tempdate.getMinutes(),
tempdate.getSeconds()
);
},
fillCalendar:function(){
var startyear = new Date().getFullYear()
//Go back 200 years
var endyear = startyear - 200;
for(var i = startyear; i > endyear; i--){
this.input.year.append('<option value="'+i+'">'+i+'</option>');
}
for(var ii = 1; ii <= 31; ii++){
this.input.day.append('<option value="'+ii+'">'+ii+'</option>');
}
for(var iii = 0; iii <= 23; iii++){
this.input.hour.append('<option value="'+iii+'">'+iii+'</option>');
}
for(var iiii = 0; iiii <= 59; iiii++){
this.input.minute.append('<option value="'+iiii+'">'+iiii+'</option>');
}
},
setBirthday:function(){
this.cal.year = this.input.year.val();
this.cal.month = this.input.month.val();
this.cal.day = this.input.day.val();
this.cal.hour = this.input.hour.val();
this.cal.minute = this.input.minute.val();
this.startDate = new Date(this.cal.year, this.cal.month, this.cal.day, this.cal.hour, this.cal.minute, 0);
this.diff = parseInt(this.now.getTime()/1000) - parseInt(this.startDate.getTime()/1000);
},
generateStats:function(){
this.updateNow();
this.setBirthday();
var diff = this.diff;
var years = (diff /(86400*365.25)).toFixed(2);
var weeks = (diff /(86400*7)).toFixed(0);
var hours = this.numberWithCommas((diff /(3600)).toFixed(1));
var days = this.numberWithCommas((diff /(86400)).toFixed(1));
var hours = this.numberWithCommas((diff /(3600)).toFixed(1));
var min = (diff /(60)).toFixed(0);
var minutes = this.numberWithCommas(min);
var seconds = this.numberWithCommas(this.diff);
$("#result").html("\
<h2>"+this.startDate+"</h2>\
<h3>You have been around for</h3>\
<p>"+years+" Years.</p>\
<p>"+weeks+" Weeks.</p>\
<p>"+days+" Days.</p>\
<p>"+hours+" Hours.</p>\
<p>"+minutes+" Minutes.</p>\
<p>"+seconds+" Seconds.</p>\
<h3>Milestones</h3>\
");
},
numberWithCommas:function(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
};
$(document)
.ready(function(){
birthcalc.init();
})
.on("change", "#calendar select", function(){
birthcalc.generateStats();
})
;