Duration

An easy, expressive and functional way of declaring a duration of time with support for various units (nanoseconds, microseconds, milliseconds, seconds, minutes, hours, and days).

// Example 1
durationOf(1000)
    .inWholeSeconds()
    .letIt(it => {
        console.log(`${(1000).milliseconds()} is the same as ${it} seconds`);
    });
// Output: 0d 0h 0m 1s 0ns is the same as 1 seconds
// Example 2
const oneYearInMinutes = (1).years().inWholeMinutes();
console.log(`1 year is ${oneYearInMinutes} minutes.`);
// Output: 1 year is 525960 minutes.
// Example 3
const duration = (1).years().add((6).months()).toString();
console.log(duration); 
// Output: 548d 0h 0m 0s 0ns
// Example 4
({name: "Manuel Santos", email: "ney.br.santos@gmail.com", age: 35})
    .letIt(it => {
        console.log(`${it.name},`);
        it.age < 30 ? console.log("A Young Man") : console.log("An Old Man");
        return it.age;
    })
    .years() // 35 as duration in years
    .runIt((days, hours, minutes, seconds, nanoseconds) => {
        return `Current Age: ${days}d ${hours}h ${minutes}m ${seconds}s ${nanoseconds}ns`;
    });
// Output:
//      Manuel Santos,
//      An Old Man
//      Current Age: 12783d 18h 0m 0s 0ns

Last updated