isomtrik-quickchat
Version:
isomtrik-quickchat is a lightweight, real-time chat component built with Stencil JS. It is designed to be seamlessly integrated into web applications, offering customizable and responsive chat functionalities. The module supports both CommonJS and ES modu
35 lines (34 loc) • 1.08 kB
JavaScript
export const formatMessageDateFromTimestamp = (timestamp) => {
if (!timestamp) {
return '';
}
const messageDate = new Date(timestamp);
const today = new Date();
today.setHours(0, 0, 0, 0);
const yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
const day = messageDate.getDate();
const month = messageDate.toLocaleString(undefined, { month: 'long' });
// Function to determine the ordinal suffix (st, nd, rd, th)
const getOrdinalSuffix = (day) => {
if (day > 3 && day < 21)
return 'th';
switch (day % 10) {
case 1: return 'st';
case 2: return 'nd';
case 3: return 'rd';
default: return 'th';
}
};
const formattedDate = `${day}${getOrdinalSuffix(day)} ${month}`;
if (messageDate >= today) {
return 'Today';
}
else if (messageDate >= yesterday && messageDate < today) {
return 'Yesterday';
}
else {
return formattedDate;
}
};
//# sourceMappingURL=getFomattedDate.js.map