@replyke/ui-core-react-native
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
49 lines • 1.52 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import moment from "moment";
import { Text } from "react-native";
function formatTime(time, justNowText) {
const now = new Date();
const then = new Date(time);
const seconds = Math.floor((now.getTime() - then.getTime()) / 1000);
let timeString = "";
if (seconds < 20) {
timeString = justNowText;
}
else if (seconds < 60) {
timeString = `${seconds}s`;
}
else if (seconds < 3600) {
timeString = `${Math.floor(seconds / 60)}m`;
}
else if (seconds < 86400) {
timeString = `${Math.floor(seconds / 3600)}h`;
}
else if (seconds < 2592000) {
// 30 days
timeString = `${Math.floor(seconds / 86400)}d`;
}
else if (seconds < 31536000) {
// 365 days
timeString = `${Math.floor(seconds / 2592000)}mo`;
}
else {
timeString = `${Math.floor(seconds / 31536000)}y`;
}
return timeString;
}
function FromNow({ time, fontSize = 12, fontWeight = 400, color = "#737373", lean, justNowText = "Just now", }) {
if (lean) {
return (_jsx(Text, { style: {
fontSize,
fontWeight: fontWeight,
color,
}, children: formatTime(time, justNowText) }));
}
return (_jsx(Text, { style: {
fontSize,
fontWeight: fontWeight,
color,
}, children: moment(new Date(time)).fromNow() }));
}
export default FromNow;
//# sourceMappingURL=FromNow.js.map