91 lines
2.1 KiB
JavaScript
Raw Normal View History

2025-12-04 20:29:35 +01:00
/**
* @fileoverview Date Formatting Utilities for Rantii
* @author retoor <retoor@molodetz.nl>
* @description Date and time formatting functions
* @keywords date, time, format, timestamp, relative
*/
function formatTimestamp(timestamp) {
const date = new Date(timestamp * 1000);
return date.toLocaleString();
}
function formatDate(timestamp) {
const date = new Date(timestamp * 1000);
return date.toLocaleDateString();
}
function formatTime(timestamp) {
const date = new Date(timestamp * 1000);
return date.toLocaleTimeString();
}
function formatRelativeTime(timestamp) {
const now = Date.now();
const time = timestamp * 1000;
const diff = now - time;
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 weeks = Math.floor(days / 7);
const months = Math.floor(days / 30);
const years = Math.floor(days / 365);
if (seconds < 60) {
return 'just now';
}
if (minutes < 60) {
return `${minutes}m ago`;
}
if (hours < 24) {
return `${hours}h ago`;
}
if (days < 7) {
return `${days}d ago`;
}
if (weeks < 4) {
return `${weeks}w ago`;
}
if (months < 12) {
return `${months}mo ago`;
}
return `${years}y ago`;
}
function formatFullDate(timestamp) {
const date = new Date(timestamp * 1000);
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
};
return date.toLocaleDateString(undefined, options);
}
function isToday(timestamp) {
const date = new Date(timestamp * 1000);
const today = new Date();
return date.toDateString() === today.toDateString();
}
function isThisWeek(timestamp) {
const date = new Date(timestamp * 1000);
const now = new Date();
const weekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
return date >= weekAgo;
}
export {
formatTimestamp,
formatDate,
formatTime,
formatRelativeTime,
formatFullDate,
isToday,
isThisWeek
};