initial page
This commit is contained in:
40
assets/js/dateUtils.js
Normal file
40
assets/js/dateUtils.js
Normal file
@@ -0,0 +1,40 @@
|
||||
function calculateDuration(startDate, endDate) {
|
||||
const start = new Date(startDate);
|
||||
const end = endDate ? new Date(endDate) : new Date();
|
||||
|
||||
const years = end.getFullYear() - start.getFullYear();
|
||||
const months = end.getMonth() - start.getMonth();
|
||||
|
||||
let totalMonths = years * 12 + months;
|
||||
if (end.getDate() < start.getDate()) {
|
||||
totalMonths--;
|
||||
}
|
||||
|
||||
const finalYears = Math.floor(totalMonths / 12);
|
||||
const finalMonths = totalMonths % 12;
|
||||
|
||||
let duration = "";
|
||||
if (finalYears > 0) {
|
||||
duration += `${finalYears} year${finalYears > 1 ? "s" : ""}`;
|
||||
}
|
||||
if (finalMonths > 0) {
|
||||
if (duration) duration += " ";
|
||||
duration += `${finalMonths} month${finalMonths > 1 ? "s" : ""}`;
|
||||
}
|
||||
return duration;
|
||||
}
|
||||
|
||||
function formatDateRange(startDate, endDate) {
|
||||
const start = new Date(startDate);
|
||||
const end = endDate ? new Date(endDate) : new Date();
|
||||
|
||||
const formatDate = (date) => {
|
||||
return date.toLocaleDateString("en-US", {
|
||||
month: "short",
|
||||
year: "numeric",
|
||||
});
|
||||
};
|
||||
|
||||
const duration = calculateDuration(startDate, endDate);
|
||||
return `${formatDate(start)} - ${endDate ? formatDate(end) : "Present"} · ${duration}`;
|
||||
}
|
||||
Reference in New Issue
Block a user