UNPKG

@replyke/ui-core-react-native

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

54 lines 1.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const moment_1 = __importDefault(require("moment")); const react_native_1 = require("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 ((0, jsx_runtime_1.jsx)(react_native_1.Text, { style: { fontSize, fontWeight: fontWeight, color, }, children: formatTime(time, justNowText) })); } return ((0, jsx_runtime_1.jsx)(react_native_1.Text, { style: { fontSize, fontWeight: fontWeight, color, }, children: (0, moment_1.default)(new Date(time)).fromNow() })); } exports.default = FromNow; //# sourceMappingURL=FromNow.js.map