Duration Build Status
This is a port of Go's Time/ParseDuration functionality.
It passes the same test cases
Note: If you don't care about the compatibility with Go checkout out the millisecond_unit branch. It makes working with Date objects much easier.
var Duration = require("./duration.js");
var d = Duration.parse("4h3m2s"); console.log( "nanoseconds", d.nanoseconds(), "\n", // => 14582000000000 "microseconds", d.microseconds(), "\n", // => 14582000000 "milliseconds", d.milliseconds(), "\n", // => 14582000 "seconds", d.seconds(), "\n", // => 14582 "minutes", d.minutes(), "\n", // => 243 "hours", d.hours(), "\n" // => 4 );
console.log( "str:", Duration.hour.toString(), "nano:", Duration.hour.valueOf() ); // => "str: 1h nano: 3600000000000"
// Addition var d1 = Duration.parse("2h"), d2 = new Duration(d1 + Duration.hour); console.log(d2.toString()) // => "3h" // Multiplication var d1 = Duration.parse("5m"), d2 = new Duration(d1 * 12); console.log(d2.toString()) // => "1h"
// Adding duration to date var d = Duration.parse("5h"), now = new Date(), later = new Date(now + d.milliseconds()); console.log(later.toString()); // Duration between two dates var bday = Date.parse("March 3, 1991"), now = new Date(), age = new Duration((now - bday) * Duration.millisecond); console.log(age.toString());