Coder Social home page Coder Social logo

duration.js's Introduction

Duration Build Status

This is a simple library for dealing with durations. It works well with javascript's Date objects.

Note: For compatibility with Go's time.Duration use the golang_compatible branch.

$ npm install duration-js
var Duration = require("duration-js");

Parse

  • ms - millisecond
  • s - second
  • m - minute
  • h - hour
  • d - day
  • w - week
var d = new Duration("6w5d4h3m2s1ms");

console.log(
    d.milliseconds(), "\n", // => 4075382001
    d.seconds(),      "\n", // => 4075382
    d.minutes(),      "\n", // => 67923
    d.hours(),        "\n", // => 1132
    d.days(),         "\n", // => 47
    d.weeks(),        "\n"  // => 6
);

Format

console.log(
  "str:",  Duration.hour.toString(),
  "ms:",   Duration.hour.valueOf()
); // => "str: 1h ms: 3600000"

Basic Operations

// Addition
var d1 = new Duration("6d"),
    d2 = new Duration(d1 + Duration.day);
console.log(d2.toString()) // => "1w"

// Multiplication
var d3 = new Duration("5m"),
    d4 = new Duration(d3 * 12);
console.log(d4.toString()) // => "1h"

// etc ...

Dates

// Adding duration to date
var d     = Duration.parse("5h"),
    now   = new Date(),
    later = new Date(now + d);
console.log(later.toString());

// Duration between two dates
var bday = new Date("March 3, 1991"),
    now  = new Date(),
    age  = new Duration(now - bday);
console.log(age.toString());

setTimeout / setInterval

setTimeout(function () {
    // runs 5 minutes later
}, new Duration("5m"));

setInterval(function () {
    // runs every 10 seconds 
}, 10 * Duration.second);

duration.js's People

Contributors

icholy avatar pdehaan avatar bitdeli-chef avatar

Watchers

Roman Rodych avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.