How to remove few seconds ago and show just now in moment.js .The moment.js shows a few seconds ago ,I want to do some customization ,to show only just now upto 1 minute.But in other cases like two minutes ago I want to show 2 minutes ago.
Example 0-59 seconds just now
import "./styles.css";
import { useEffect } from "react";
import moment from "moment";
export default function App() {
moment.updateLocale("en", {
relativeTime: {
future: "in %s",
past: "%s ago",
s: " %ds",
ss: "%d seconds",
m: "%dm",
mm: "%dm",
h: "an h",
hh: "%dh",
d: "a day",
dd: "%d days",
w: "a week",
ww: "%d weeks",
M: "a month",
MM: "%d months",
y: "a year",
yy: "%d years"
}
});
const MINUTE_MS = 60000;
useEffect(() => {
const interval = setInterval(() => {
console.log("Logs every minute");
called();
}, MINUTE_MS);
return () => clearInterval(interval); // This represents the unmount function, in which you need to clear your interval to prevent memory leaks.
}, []);
const called = () => {
var k = Date.now() - 5 * 1000;
return k;
};
moment.locale("en");
return (
<div className="App">
<div>{moment(called()).fromNow()}</div>
</div>
);
}
You could set s and ss to "just now" and change both future and past into a function that doesn't prefix/suffix if the passed string is "just now".
moment.updateLocale("en", {
relativeTime: {
future: (diff) => (diff == "just now" ? diff : `in ${diff}`),
past: (diff) => (diff == "just now" ? diff : `${diff} ago`),
s: "just now",
ss: "just now"
}
});
const _2_min_ago = moment().subtract(2, "minutes");
const _20_sec_ago = moment().subtract(20, "seconds");
const _20_sec_from_now = moment().add(20, "seconds");
const _2_min_from_now = moment().add(2, "minutes");
console.log(_2_min_ago.fromNow());
console.log(_20_sec_ago.fromNow());
console.log(_20_sec_from_now.fromNow());
console.log(_2_min_from_now.fromNow());
<script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>