I'm actually building a chat app and using react and when I send a message it takes my local time and saved it to the database and using socket io library the receiver will get the message the problem is first it will work fine if both sender and receiver have the same timezone but if they have different timezones it will say just now for the sender but for the receiver 5 hours ago how to fix that is there any library or is a way to fix the code
and also I'm using timeago.js to format the timestamp like, just now,4 hours ago...
//sender side
const socketMessage = {
_id:uuid(),
conversationId,
senderId: user?._id,
textMessage,
username: currentChat.username,
createdAt: new Date(),
senderUsername: user?.username,
};
socket.emit("sendMessage", socketMessage);
//receiver side
useEffect(() => {
let mounted = true;
socket?.on("getMessage", (data) => {
if (mounted) {
setArrivalMessage({
_id: data._id,
conversationId: data.conversationId,
senderId: data.senderId,
textMessage: data?.textMessage,
createdAt: data.createdAt,
});
}
});
return () => {
mounted = false;
};
}, [socket]);