1

So I'm making a userinfo command

I use moment.js for relative time

moment(timestamp).fromNow() \\ 2 years ago

Is there a way to format it like 2 years x days, x seconds ago etc

enter image description here

Azer
  • 446
  • 2
  • 8
  • I can't give you an exact answer to what you want (would have to spend some time that I don't have now), but yes, you can customize the thresholds for relative times in `moment`. See [this](https://momentjscom.readthedocs.io/en/latest/moment/07-customization/13-relative-time-threshold/) for more information. You'll have to find how you want to make it exactly how you want though. Other way could be using the default thresholds and concatenate the different parts (getting the string for "2 years", substract the years, then get "8 months", then substract the months, and get "16 days ago") – Jcl Jan 19 '22 at 21:07
  • 1
    [I would refrain from using momentjs](https://momentjs.com/docs/#/-project-status/) and instead use another library or roll your own implementation such as https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site – Cjmarkham Jan 19 '22 at 21:08
  • Have you considered [*Difference between two dates in years, months, days in JavaScript*](https://stackoverflow.com/questions/17732897/difference-between-two-dates-in-years-months-days-in-javascript)? – RobG Jan 20 '22 at 02:57
  • Also, to format intervals with Luxon (successor to Moment.js) see [*this answer*](https://stackoverflow.com/a/65651515/257182). – RobG Jan 20 '22 at 03:36

2 Answers2

2

I don't think there's an easy way to do this with Moment.js.

However, using Luxon (successor to Moment.js) you can create an interval, convert it to a duration then format the values. E.g. to get the years, months and days from the ECMAScript epoch to now:

// Create an interval from 1 Jan 1970 UTC to now
let interval = luxon.Interval.fromDateTimes(
  new Date(0), // 1 Jan 1970 UTC
  new Date()   // now
);

// Show default interval object
console.log(interval.toFormat('yyyy-MM-dd'));

// Convert to duration object
let d = interval.toDuration(['years','months','days']).toObject();

// Display in friendly format
let text = [];
d.years? text.push(d.years + ' years') : null;
d.months? text.push(d.months + ' months') : null;
d.days? text.push(d.days.toFixed(1) + ' days') : null;

console.log(text.join(', '));
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.0/luxon.min.js"></script>

Note there is also:

  1. Luxon interval human readable
  2. Difference between two dates in years, months, days in JavaScript
  3. How to get difference between 2 Dates in Years, Months and days using moment.js

and many others.

RobG
  • 142,382
  • 31
  • 172
  • 209
-2

You mean, like moment.fromNow()

[Sometimes, it pays to read the documentation]

If that won't work for you, then something like

might.

But...

Consider migrating to Luxon — Moment.js is deprecated. Luxon is smaller, more elegant. More to point, it has a Duration that has a toFormat() method that will give you exactly what you are looking for.

duration.toFormat() docs

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • I checked the docs and knew there was no option to do so, `fromNow()` didn't format in the way I wanted it to that's why I asked for suggestions\ – Azer Jan 20 '22 at 09:23