forked from AC6567/geraldanekwe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateCounter.js
More file actions
50 lines (36 loc) · 1.08 KB
/
dateCounter.js
File metadata and controls
50 lines (36 loc) · 1.08 KB
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
var age = function(year, month, day){
var today = new Date();
var todayMS = Date.UTC(today.getFullYear(), today.getMonth(), today.getDate());
var inputDate = new Date(year, month, day);
var inputDateMS = Date.UTC(inputDate.getFullYear(), (inputDate.getMonth() - 1), inputDate.getDate());
var msInOneDay = 1000 * 60 * 60 * 24;
var daysTotal = ((inputDateMS - todayMS) / msInOneDay);
var yearCount = 0;
var monthCount = 0;
var dayCount = 0;
var oneMonth = 365.25 / 12;
var oneYear = 365.25;
if(daysTotal < oneMonth){
daysTotal++;
}
while(daysTotal > oneYear){
daysTotal = daysTotal - oneYear;
yearCount++;
}
if(daysTotal > oneMonth){
while(daysTotal > oneMonth){
daysTotal = daysTotal - oneMonth;
monthCount++;
}
}
if(yearCount === 0){
console.log(monthCount + " month(s), " + Math.round(daysTotal - 1) + " day(s)" );
}
else if(yearCount === 0 & monthCount === 0){
console.log(Math.round(daysTotal - 1) + " day(s)");
}
else{
console.log(yearCount + " year(s), " + monthCount + " month(s), " + Math.round(daysTotal - 1) + " day(s).");
}
};
age(2017, 12, 17);