-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
108 lines (64 loc) · 2.09 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
var pdiv=document.createElement('div');
pdiv.setAttribute('style','padding-top:20px');
var datelem=document.createElement('input');
datelem.setAttribute('type','date');
datelem.setAttribute('id','dob');
datelem.setAttribute('name','dob');
pdiv.appendChild(datelem);
var button=document.createElement('button');
button.innerHTML="display data";
button.setAttribute('class','btn btn-primary');
button.addEventListener('click',calculate);
pdiv.appendChild(button);
var dd=document.createElement('div');
dd.setAttribute('id','display');
function calculate(){
var input=document.getElementById('dob').value;
console.log(input);
if(Date.parse(input)){
var inputdate=new Date(input);
console.log(inputdate);
var currentdate= new Date();
console.log(currentdate);
var millisecondsdiff=parseInt(currentdate.getTime())-parseInt(inputdate.getTime());
console.log(millisecondsdiff);
var secondsdiff=mathfloor(millisecondsdiff,1000);
console.log(secondsdiff);
var minutediff=mathfloor(secondsdiff,60);
console.log(minutediff);
var hourdiff=mathfloor(minutediff,60);
console.log(hourdiff);
var daydiff=mathfloor(hourdiff,24);
console.log(daydiff);
var yeardiff=getyear(inputdate,currentdate);
console.log(yeardiff);
var monthdiff=getmonth(inputdate,currentdate);
console.log(monthdiff);
dd.innerHTML=`given inpu date is :${inputdate} <br>
year :${yeardiff} <br>
month: ${monthdiff} <br>
day :${daydiff} <br>
hours :${hourdiff} <br>
minutes :${minutediff}<br>
seconds :${secondsdiff} <br>
milliseconds:${millisecondsdiff}`;
}
else{
dd.innerHTML="invalid date";
}
document.body.appendChild(dd);
}
function mathfloor(value1,value2){
return Math.floor(value1/value2);
}
function getyear(value1,value2){
var date1=new Date(value1);
var date2=new Date(value2);
return date2.getFullYear()-date1.getFullYear();
}
function getmonth(value1,value2){
var year=getyear(value1,value2);
var month=(year*12)+(value2.getMonth()-value1.getMonth());
return month;
}
document.body.append(pdiv);