Duration
This is a library for dealing with durations.
It works well with javascript's Date objects.
$ npm install @icholy/duration
import { Duration } from "@icholy/duration";
Parse
ms
- milliseconds
- secondm
- minuteh
- hourd
- dayw
- week
var d = new Duration("6w5d4h3m2s1ms");
console.log(
d.milliseconds(), "\n",
d.seconds(), "\n",
d.minutes(), "\n",
d.hours(), "\n",
d.days(), "\n",
d.weeks(), "\n"
);
Format
console.log(
"str:", Duration.hour.toString(),
"ms:", Duration.hour.valueOf()
);
Basic Operations
var d1 = new Duration("6d"),
d2 = new Duration(d1 + Duration.day);
console.log(d2.toString())
var d3 = new Duration("5m"),
d4 = new Duration(d3 * 12);
console.log(d4.toString())
Dates
var d = Duration.parse("5h"),
now = new Date(),
later = new Date(now + d);
console.log(later.toString());
var bday = new Date("March 3, 1991"),
now = new Date(),
age = new Duration(now - bday);
console.log(age.toString());
setTimeout / setInterval
setTimeout(function () {
}, new Duration("5m"));
setInterval(function () {
}, 10 * Duration.second);