58 lines
2.1 KiB
JavaScript
58 lines
2.1 KiB
JavaScript
window.addEventListener('DOMContentLoaded', function() {
|
|
updateDates();
|
|
}, false);
|
|
|
|
function updateDates() {
|
|
dateElements = document.querySelectorAll(".dateConverter");
|
|
const options = {
|
|
weekday: "long",
|
|
year: "numeric",
|
|
month: "long",
|
|
day: "numeric",
|
|
hour: "2-digit",
|
|
minute: "2-digit"
|
|
};
|
|
dateElements.forEach(element => {
|
|
dateToConvert = element.innerHTML + ' UTC';
|
|
newDate = new Date(dateToConvert).toLocaleDateString('no-NB', options);
|
|
timeAgo(dateToConvert);
|
|
element.innerHTML = 'Startet ' + timeAgo(dateToConvert);
|
|
});
|
|
}
|
|
|
|
function timeAgo(dateString)
|
|
{
|
|
// Assume we have a date string being received from some object.
|
|
const data = {
|
|
formattedDate: dateString // Example date string in ISO 8601 format
|
|
};
|
|
|
|
// Extract the formatted date string from the object
|
|
const { formattedDate } = data;
|
|
|
|
// Convert the date string to a Date object
|
|
const dateObject = new Date(formattedDate);
|
|
|
|
// Calculate the difference in seconds between the given date and the current date
|
|
const secondsDiff = Math.round((dateObject - Date.now()) / 1000);
|
|
|
|
// Array representing one minute, hour, day, week, month, etc. in seconds
|
|
const unitsInSec = [60, 3600, 86400, 86400 * 7, 86400 * 30, 86400 * 365, Infinity];
|
|
|
|
// Array equivalent to the above but in the string representation of the units
|
|
const unitStrings = ["second", "minute", "hour", "day", "week", "month", "year"];
|
|
|
|
// Find the appropriate unit based on the seconds difference
|
|
const unitIndex = unitsInSec.findIndex((cutoff) => cutoff > Math.abs(secondsDiff));
|
|
|
|
// Get the divisor to convert seconds to the appropriate unit
|
|
const divisor = unitIndex ? unitsInSec[unitIndex - 1] : 1;
|
|
|
|
// Initialize Intl.RelativeTimeFormat
|
|
const rtf = new Intl.RelativeTimeFormat("no", { numeric: "auto" });
|
|
|
|
// Format the relative time based on the calculated unit
|
|
const relativeTime = rtf.format(Math.floor(secondsDiff / divisor), unitStrings[unitIndex]);
|
|
|
|
return(relativeTime);
|
|
} |