-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
71 lines (61 loc) · 2.64 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Birth Year</title>
</head>
<body>
<center>
<form style="margin-top: 100px;">
<label for="number">Age</label>
<input style="height: 30px; width: 400px; padding-left: 10px; margin-left: 70px;" type="number" name="number" id="number" placeholder="Type Your Age...">
<input style="height: 35px; margin-bottom: 15px;" type="button" value="Find Year" id="button"></br>
<label for="date">Date Of Birth</label>
<input style="height: 30px; width: 400px; padding-left: 10px; margin-left: 8px" type="date" name="date" id="date" placeholder="Type Your Date of Birth">
<input style="height: 35px; width: 76px;" type="button" value="Get Age" id="dateofbirth">
<center>
<p id="year">Year</p>
</center>
</form>
</center>
<script type="text/javascript">
function calculate(){
var number = document.getElementById("number").value;
if(number == "" || number <= 0){
window.alert("Please type your correct age!");
return;
}
var date = new Date().getFullYear();
var birthyear = date - number;
document.getElementById("year").style.display = "block";
document.getElementById("year").innerHTML = "Your birth year is "+birthyear;
}
document.getElementById("year").style.display = "none";
document.getElementById("button").onclick = function(){
calculate();
}
document.getElementById("dateofbirth").onclick = function(){
var DOB = date.value;
var age = 0;
if(DOB === ""){
window.alert("Please type your correct date of birth");
return;
}
var DOBYear = parseInt(DOB.split("-")[0]);
var DOBDay = parseInt(DOB.split("-")[2]);
var DOBMonth = parseInt(DOB.split("-")[1]);
var year = new Date().getFullYear();
var month = new Date().getMonth() + 1;
var day = new Date().getDate();
age = year - DOBYear - 1;
if(month > DOBMonth || (day >= DOBDay && month >= DOBMonth)){
age++;
}
document.getElementById("year").style.display = "block";
document.getElementById("year").innerHTML = "Your age is "+age;
}
</script>
</body>
</html>