mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-04-19 19:01:19 +00:00
49 lines
1.6 KiB
JavaScript
49 lines
1.6 KiB
JavaScript
export const formatUnixTimestamp = (time) => {
|
|
const date = new Date(time * 1000); // Convert to milliseconds
|
|
return date.toDateString();
|
|
}
|
|
|
|
export const formatDateTime = (isoDate) => {
|
|
const date = new Date(isoDate);
|
|
|
|
// Example: Format to a more readable string
|
|
const formattedDate = date.toLocaleString("en-US", {
|
|
timeZone: "UTC", // Optional: You can change this to the user's time zone if needed
|
|
weekday: "long", // "long" for full name, "short" for abbreviated
|
|
year: "numeric",
|
|
month: "long", // "long" for full name, "short" for abbreviated
|
|
day: "numeric",
|
|
hour: "numeric",
|
|
minute: "numeric",
|
|
second: "numeric",
|
|
});
|
|
|
|
return formattedDate;
|
|
}
|
|
|
|
export const formatTimestampToHowLongAgo = (time) => {
|
|
const date = new Date(time * 1000);
|
|
const now = new Date();
|
|
const diff = now - date;
|
|
const seconds = Math.floor(diff / 1000);
|
|
const minutes = Math.floor(seconds / 60);
|
|
const hours = Math.floor(minutes / 60);
|
|
const days = Math.floor(hours / 24);
|
|
const months = Math.floor(days / 30);
|
|
const years = Math.floor(months / 12);
|
|
|
|
if (years > 0) {
|
|
return `${years} year${years > 1 ? 's' : ''} ago`;
|
|
} else if (months > 0) {
|
|
return `${months} month${months > 1 ? 's' : ''} ago`;
|
|
} else if (days > 0) {
|
|
return `${days} day${days > 1 ? 's' : ''} ago`;
|
|
} else if (hours > 0) {
|
|
return `${hours} hour${hours > 1 ? 's' : ''} ago`;
|
|
} else if (minutes > 0) {
|
|
return `${minutes} minute${minutes > 1 ? 's' : ''} ago`;
|
|
} else {
|
|
return `${seconds} second${seconds > 1 ? 's' : ''} ago`;
|
|
}
|
|
}
|