-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaysBetweenDatesIncludingYears.js
128 lines (97 loc) · 3.19 KB
/
daysBetweenDatesIncludingYears.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
// assuming non leap years, it has no limit on the number of days
window.addEventListener('load', onLoad);
let firstDate, secondDate;
let date1 = [20, 6, 2023];
let date2 = [24, 7, 2006];
function onLoad(){
daysBetweenDates(date1, date2);
}
function daysBetweenDates(date1, date2){
let paragraph = addElement('p', document.body);
if(date1[2]==date2[2]){
if(date1[1]>date2[1]){
firstDate = date2;
secondDate = date1;
} else if(date2[1]>date1[1]){
firstDate = date1;
secondDate = date2;
} else if(date1[1]==date2[1]){
if(date1[0]>date2[0]){
secondDate = date1;
firstDate = date2;
} else{
firstDate = date2;
secondDate = date1;
}
}
console.log(daysBetweenMonthsInSameYear(firstDate, secondDate));
paragraph.innerText = daysBetweenMonthsInSameYear(firstDate, secondDate);
}
if(date1[2]>date2[2]){
firstDate = date2;
secondDate = date1;
console.log(daysBetweenDifferentYears(firstDate, secondDate));
paragraph.innerText = daysBetweenDifferentYears(firstDate, secondDate);
} else if(date2[2]>date1[2]){
firstDate = date1;
secondDate = date2;
console.log(daysBetweenDifferentYears(firstDate, secondDate));
paragraph.innerText = daysBetweenDifferentYears(firstDate, secondDate);
}
}
function daysBetweenMonthsInSameYear(firstDate, secondDate){
let totalDays = 0;
let daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// if(firstDate[2] % 4 == 0){
// daysInMonths[1]++;
// }
// if(secondDate[2] % 4 == 0){
// daysInMonths[1]++;
// }
if(firstDate[1] == secondDate[1]){
totalDays = secondDate[0] - firstDate[0];
} else{
for(let i=firstDate[1]; i<secondDate[1]-1; i++){
totalDays += daysInMonths[i] ;
}
totalDays += secondDate[0] + daysInMonths[firstDate[1]-1] - firstDate[0];
}
totalDays += (secondDate[2]-firstDate[2])*365;
return totalDays;
}
function daysBetweenDifferentYears(firstDate, secondDate){
let firstDateNew, secondDateNew;
let daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let totalDaysFinal = 0;
for(let i=firstDate[1]; i<daysInMonths.length; i++){
totalDaysFinal += daysInMonths[i];
}
for(let i=0; i<secondDate[1]-1; i++){
totalDaysFinal += daysInMonths[i];
}
if(date1[1]>date2[1]){
firstDateNew = date2;
secondDateNew = date1;
} else if(date2[1]>date1[1]){
firstDateNew = date1;
secondDateNew = date2;
} else if(date1[1]==date2[1]){
if(date1[0]>date2[0]){
secondDateNew = date1;
firstDateNew = date2;
} else{
firstDateNew = date2;
secondDateNew = date1;
}
}
totalDaysFinal += daysInMonths[firstDateNew[1]-1] - firstDateNew[0] + secondDateNew[0];
if(secondDate[2]-firstDate[2]>1){
totalDaysFinal += (secondDate[2]-firstDate[2]-1)*365;
}
return totalDaysFinal;
}
function addElement(elementTag, parent){
let element = document.createElement(elementTag);
parent.appendChild(element);
return element;
}