Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {DateTime} from "ts-luxon";
- function humanizedDuration(d1, d2, maxUnit = "days") {
- const units = [
- "weeks", "days", "hours", "minutes", "seconds"
- ];
- const maxUnitIndex = maxUnit.indexOf(maxUnit);
- const diff = DateTime
- .fromJSDate(rawJsDate)
- .diff(DateTime.fromMillis(+rawJsDate))
- .toFormat(
- ["ww", "dd", "hh", "mm", "ss"]
- .slice(maxUnitIndex)
- .join(" ")
- )
- .split(" ");
- return units
- .slice(maxUnitIndex)
- .map((u, i) => `${diff[i]}${u.slice(0, 1)}`)
- .filter((v, i) => !~["weeks", "days"].indexOf(v) || diff[i] !== 0)
- .join(" ")
- }
- const now = new Date();
- console.info(humanizedDuration(
- now, new Date(+now +(1000*3600*48)))
- );
- // output: 1d 23h 59m 59s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement