-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.js
128 lines (79 loc) · 2.98 KB
/
life.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
120
121
122
123
124
125
126
127
128
//let isdobopen=false;
let dateofbirth;
const settingicon=document.getElementById("icon");
const settingcontent=document.getElementById("settingContent");
const initialText=document.getElementById("initial");
const afterdobbtn=document.getElementById("afterdobbutton");
const dobbuttontxt=document.getElementById("dobbutton");
const dobinput=document.getElementById("dob");
const yearEl=document.getElementById("year");
const monthEl=document.getElementById("month");
const dayEl=document.getElementById("day");
const hourEl=document.getElementById("hour");
const minuteEl=document.getElementById("minute");
const secondEl=document.getElementById("second");
const makeTwodigitnumber = (number) => {
return number > 9 ? number : `0${number}`;
};
//used for only setting icon which is used like toggle
const toggleDateofbirth = () => {
settingcontent.classList.toggle("hide");
// if(isdobopen){
// settingcontent.classList.add("hide");
// }else{
// settingcontent.classList.remove("hide");
// }
// isdobopen = !isdobopen;
};
const updateage = () => {
const currentDate = new Date();
const datediff= currentDate-dateofbirth;
const year=Math.floor(datediff/(1000*60*60*24*365));
const month=Math.floor(datediff/(1000*60*60*24*365))%12;
const day=Math.floor(datediff/(1000*60*60*24))%30;
const hour=Math.floor(datediff/(1000*60*60))%24;
const minutes=Math.floor(datediff/(1000*60))%60;
const second=Math.floor(datediff/(1000))%60;
yearEl.innerHTML=makeTwodigitnumber(year);
monthEl.innerHTML=makeTwodigitnumber(month);
dayEl.innerHTML=makeTwodigitnumber(day);
hourEl.innerHTML=makeTwodigitnumber(hour);
minuteEl.innerHTML=makeTwodigitnumber(minutes);
secondEl.innerHTML=makeTwodigitnumber(second);
};
const localstoragegetter = () =>{
const year=localStorage.getItem("year");
const month=localStorage.getItem("month");
const date=localStorage.getItem("date");
if(year && month && date){
dateofbirth = new Date(year,month,date);
}
updateage();
};
const contentToggler = () =>{
updateage();
if(dateofbirth){
initialText.classList.add("hide");
afterdobbtn.classList.remove("hide");
setInterval(()=>updateage(),1000);
}else{
afterdobbtn.classList.add("hide");
initialText.classList.remove("hide");
}
};
const setDOBHandler = () => {
const dateString = dobinput.value;
dateofbirth=dateString ? new Date(dateString) : null;
if(dateofbirth){
localStorage.setItem("year",dateofbirth.getFullYear());
localStorage.setItem("month",dateofbirth.getMonth());
localStorage.setItem("date",dateofbirth.getDate());
}
setInterval(()=>updateage(),1000);
contentToggler();
};
localstoragegetter();
contentToggler();
settingicon.addEventListener("click",toggleDateofbirth);
dobbuttontxt.addEventListener("click",setDOBHandler);
//now we will used for timer