Skip to content

Commit

Permalink
Update updateDate.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ThakshilaDamsak authored Nov 20, 2023
1 parent f8a11d3 commit 988c40c
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions docs/updateDate.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
// Check if the elements already exist
const timePassedElement = document.getElementById("kraftbin-relative-time");
const dateElement = document.getElementById("kraftbin-absolute-time");
// Check if the elements already exist
const kraftbinTimePassedElement = document.getElementById("kraftbin-relative-time");
const kraftbinDateElement = document.getElementById("kraftbin-absolute-time");

if (timePassedElement && dateElement) {
// Get the current date
const currentDate = new Date();
if (kraftbinTimePassedElement && kraftbinDateElement) {
// Get the current date
const currentDate = new Date();

// Get the last modified date of the page
const lastModifiedDate = new Date(document.lastModified);
// Get the last modified date of the page
const lastModifiedDate = new Date(document.lastModified);

// Format the date as "yyyy.MM.dd at hh:mm a" (using user's time zone)
const options = { year: 'numeric', month: '2-digit', day: '2-digit', hour: 'numeric', minute: 'numeric', hour12: true };
const formattedDate = lastModifiedDate.toLocaleString(undefined, options);
// Format the date as "yyyy.MM.dd at hh:mm a" (using user's time zone)
const options = { year: 'numeric', month: '2-digit', day: '2-digit', hour: 'numeric', minute: 'numeric', hour12: true };
const formattedDate = lastModifiedDate.toLocaleString(undefined, options);

// Calculate the time difference in milliseconds
const timeDifference = currentDate - lastModifiedDate;
// Calculate the time difference in milliseconds
const timeDifference = currentDate - lastModifiedDate;

// Define time intervals in milliseconds
const minute = 60 * 1000;
const hour = 60 * minute;
const day = 24 * hour;
const week = 7 * day;
const month = 30 * day;
// Define time intervals in milliseconds
const minute = 60 * 1000;
const hour = 60 * minute;
const day = 24 * hour;
const week = 7 * day;
const month = 30 * day;

// Calculate the relative time based on the time difference
let relativeTime;
if (timeDifference < minute) {
relativeTime = "just now.";
} else if (timeDifference < hour) {
const minutes = Math.floor(timeDifference / minute);
relativeTime = `${minutes} minute${minutes > 1 ? "s" : ""} ago.`;
} else if (timeDifference < day) {
const hours = Math.floor(timeDifference / hour);
relativeTime = `${hours} hour${hours > 1 ? "s" : ""} ago.`;
} else if (timeDifference < week) {
const days = Math.floor(timeDifference / day);
relativeTime = `${days} day${days > 1 ? "s" : ""} ago.`;
} else if (timeDifference < month) {
const weeks = Math.floor(timeDifference / week);
relativeTime = `${weeks} week${weeks > 1 ? "s" : ""} ago.`;
} else {
const months = Math.floor(timeDifference / month);
relativeTime = `${months} month${months > 1 ? "s" : ""} ago.`;
}
// Calculate the relative time based on the time difference
let kraftbinRelativeTime;
if (timeDifference < minute) {
kraftbinRelativeTime = "just now.";
} else if (timeDifference < hour) {
const minutes = Math.floor(timeDifference / minute);
kraftbinRelativeTime = `${minutes} minute${minutes > 1 ? "s" : ""} ago.`;
} else if (timeDifference < day) {
const hours = Math.floor(timeDifference / hour);
kraftbinRelativeTime = `${hours} hour${hours > 1 ? "s" : ""} ago.`;
} else if (timeDifference < week) {
const days = Math.floor(timeDifference / day);
kraftbinRelativeTime = `${days} day${days > 1 ? "s" : ""} ago.`;
} else if (timeDifference < month) {
const weeks = Math.floor(timeDifference / week);
kraftbinRelativeTime = `${weeks} week${weeks > 1 ? "s" : ""} ago.`;
} else {
const months = Math.floor(timeDifference / month);
kraftbinRelativeTime = `${months} month${months > 1 ? "s" : ""} ago.`;
}

// Update the content of the <span> elements
timePassedElement.textContent = relativeTime;
dateElement.textContent = formattedDate;
};
// Update the content of the <span> elements
kraftbinTimePassedElement.textContent = kraftbinRelativeTime;
kraftbinDateElement.textContent = formattedDate;
};

0 comments on commit 988c40c

Please sign in to comment.