@intility/bifrost-react
Version:
React library for Intility's design system, Bifrost.
28 lines (27 loc) • 1.01 kB
JavaScript
// TypeScript is missing types for Intl.DurationFormat. should be fixed in:
// https://github.com/microsoft/TypeScript/issues/60608
// https://github.com/microsoft/TypeScript/pull/60646
// until then, reproduce relevant parts locally
/**
* Construct a Duration object based on start and end dates. Used internally by `<FormatDuration>`
* @param start Start date
* @param end End date
* @returns object with days, hours, or minutes meant to be used by `Intl.DurationFormat().format()`
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/format
*/
export default function getDuration(start, end) {
const durationInMs = end.getTime() - start.getTime();
const minutes = Math.round(durationInMs / 1000 / 60);
const hours = minutes / 60;
const days = hours / 24;
if (days >= 2 || days <= -2) return {
days: Math.round(days)
};
if (hours >= 2 || hours <= -2) return {
hours: Math.round(hours)
};
return {
minutes
};
}