Method to convert miliseconds to duration

Hi there

I’m looking for a (build-in?) method that converts a number in miliseconds to a human readable duration like “2h 30m” or “46m”. I see other core plugins do that too, so i checked the API Reference but only found the parseDuration method, which seems to do the exact opposite.

Please give me a hint.

Regards
Raphael

The easiest is probably dateTime which uses momentjs.
There is also the builtin Date.

const milliseconds = Date.now();

const grafanaDate = dateTime(milliseconds).toString(); // Uses https://momentjs.com/ (Basically the same as dateTimeAsMoment)
const builtInDate = new Date(milliseconds).toString(); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
console.log(grafanaDate); // Thu Apr 07 2022 08:14:44 GMT+0200
console.log(builtInDate); //Thu Apr 07 2022 08:14:44 GMT+0200 (Central European Summer Time)

const formattedDate = dateTime(milliseconds).format('DD/MM/YY');
console.log(formattedDate); // 07/04/22
1 Like

Thank you for your message @zuperzee! But that don’t really answers my question. I would like to have a duration and not a date in human readable format.

Found the solution. I use the humanize() method from moment now:

`moment.duration(value as number).humanize();`

Without a second parameter the unit defaults to miliseconds.

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.