Skip to content

Commit 8fea39c

Browse files
authored
updated formula for duration
1 parent 5d54972 commit 8fea39c

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

index.html

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,44 +1319,47 @@ <h4>Call:</h4>
13191319
</script>
13201320

13211321
<script>
1322-
function calculateDuration(start, end) {
1323-
const [startYear, startMonth] = start.split('-').map(Number);
1324-
let endDate;
1325-
1326-
if (end.toLowerCase() === 'present') {
1327-
endDate = new Date();
1328-
} else {
1329-
const [endYear, endMonth] = end.split('-').map(Number);
1330-
endDate = new Date(endYear, endMonth - 1);
1322+
function calculateDuration(start, end) {
1323+
const [startYear, startMonth] = start.split('-').map(Number);
1324+
let endDate;
1325+
1326+
if (end.toLowerCase() === 'present') {
1327+
const now = new Date();
1328+
endDate = new Date(now.getFullYear(), now.getMonth());
1329+
} else {
1330+
const [endYear, endMonth] = end.split('-').map(Number);
1331+
endDate = new Date(endYear, endMonth - 1);
1332+
}
1333+
1334+
const startDate = new Date(startYear, startMonth - 1);
1335+
1336+
// Include both start and end month by adding 1
1337+
let months = (endDate.getFullYear() - startDate.getFullYear()) * 12
1338+
+ (endDate.getMonth() - startDate.getMonth()) + 1;
1339+
1340+
const years = Math.floor(months / 12);
1341+
months = months % 12;
1342+
1343+
let result = "(";
1344+
if (years > 0) result += `${years} year${years > 1 ? 's' : ''}`;
1345+
if (years > 0 && months > 0) result += " ";
1346+
if (months > 0) result += `${months} month${months > 1 ? 's' : ''}`;
1347+
if (years === 0 && months === 0) result += "less than a month";
1348+
result += ")";
1349+
1350+
return result;
13311351
}
13321352

1333-
const startDate = new Date(startYear, startMonth - 1);
1334-
let months = (endDate.getFullYear() - startDate.getFullYear()) * 12 + (endDate.getMonth() - startDate.getMonth());
1353+
document.addEventListener("DOMContentLoaded", () => {
1354+
const durations = document.querySelectorAll(".duration");
13351355

1336-
const years = Math.floor(months / 12);
1337-
months = months % 12;
1338-
1339-
let result = "(";
1340-
if (years > 0) result += `${years} year${years > 1 ? 's' : ''}`;
1341-
if (years > 0 && months > 0) result += " ";
1342-
if (months > 0) result += `${months} month${months > 1 ? 's' : ''}`;
1343-
if (years === 0 && months === 0) result += "less than a month";
1344-
result += ")";
1345-
1346-
return result;
1347-
}
1348-
1349-
document.addEventListener("DOMContentLoaded", () => {
1350-
const durations = document.querySelectorAll(".duration");
1351-
1352-
durations.forEach(span => {
1353-
const start = span.getAttribute("data-start");
1354-
const end = span.getAttribute("data-end");
1355-
span.textContent = ` ${calculateDuration(start, end)}`;
1356+
durations.forEach(span => {
1357+
const start = span.getAttribute("data-start");
1358+
const end = span.getAttribute("data-end");
1359+
span.textContent = ` ${calculateDuration(start, end)}`;
1360+
});
13561361
});
1357-
});
1358-
</script>
1359-
1362+
</script>
13601363
<!-- ======= Mobile nav toggle button ======= -->
13611364
<!-- Vendor JS Files -->
13621365
<script src="assets/vendor/purecounter/purecounter_vanilla.js"></script>

0 commit comments

Comments
 (0)