Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
Replace a few seconds ago with just now in moment.js

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>
  );
}

       
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!