Paragraph on home page has said "For the past two weeks..." for almost two months. I quickly tried to get something together but didn't set up a test environment to try it.
<span id="elapsed-time"></span>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const previousDate = new Date('2024-07-04'); // Set your previous date here
const today = new Date();
const elapsedTime = getElapsedTime(previousDate, today);
document.getElementById('elapsed-time').innerText = elapsedTime;
});
function getElapsedTime(startDate, endDate) {
const msPerDay = 24 * 60 * 60 * 1000;
const msPerMonth = 30 * msPerDay;
const msPerYear = 365 * msPerDay;
const elapsed = endDate - startDate;
if (elapsed < msPerMonth) {
const days = Math.floor(elapsed / msPerDay);
return `For the past ${days} ${days === 1 ? 'day' : 'days'}`;
} else if (elapsed < msPerYear) {
const months = Math.floor(elapsed / msPerMonth);
return `For the past ${months} ${months === 1 ? 'month' : 'months'}`;
} else {
const years = Math.floor(elapsed / msPerYear);
return `For the past ${years} ${years === 1 ? 'year' : 'years'}`;
}
}
</script>
Paragraph on home page has said "For the past two weeks..." for almost two months. I quickly tried to get something together but didn't set up a test environment to try it.
<span id="elapsed-time"></span><script> document.addEventListener('DOMContentLoaded', (event) => { const previousDate = new Date('2024-07-04'); // Set your previous date here const today = new Date(); const elapsedTime = getElapsedTime(previousDate, today); document.getElementById('elapsed-time').innerText = elapsedTime; }); function getElapsedTime(startDate, endDate) { const msPerDay = 24 * 60 * 60 * 1000; const msPerMonth = 30 * msPerDay; const msPerYear = 365 * msPerDay; const elapsed = endDate - startDate; if (elapsed < msPerMonth) { const days = Math.floor(elapsed / msPerDay); return `For the past ${days} ${days === 1 ? 'day' : 'days'}`; } else if (elapsed < msPerYear) { const months = Math.floor(elapsed / msPerMonth); return `For the past ${months} ${months === 1 ? 'month' : 'months'}`; } else { const years = Math.floor(elapsed / msPerYear); return `For the past ${years} ${years === 1 ? 'year' : 'years'}`; } } </script>