timespanjs

0.2.3 • Public • Published

timespan

Build Status

Timespan is a chainable object you can use to represent a span of time for interacting with the js Date object in node.js, and the browser.

Install via npm

    npm install timespanjs

Use in browser

    <script src="bin/timespan.min.js"></script>

Import into your file and create a timespan

   var Timespan = require('timespanjs');
   var ts = new Timespan();
 
   //pass it a starting time
   var ts = new Timespan(1234);
 
   //pass it another timespan object
   var ts1 = new Timespan(1234);
   var ts2 = new Timespan(ts1);

Add time

   ts.addMinute(1);
   ts.addSecond(2).addHour(3).addDay(4);

Add to a date

 
   var date = new Date();
   date = ts.addToDate(date);
 
   //or
   date.addTimespan(ts);
 

display as a string

   var ts = new Timespan().addHour(2).addMinute(1).addSecond(13);
 
   //returns '2 hours 1 minute 13 seconds'
   ts.toString();
 

Timespan is very useful for intervals and timeouts

    var everyThirtyMinutes = new Timespan().addMinutes(30);
    var intervalId = everyThirtyMinutes.setInterval(function(){
        console.log('interval!');
    });
    clearInterval(intervalId);
 
    var timeoutId = everyThirtyMinutes.setTimeout(function(){
        console.log('timeout!');
    });
 
    clearTimeout(timeoutId);
 

Readme

Keywords

none

Package Sidebar

Install

npm i timespanjs

Weekly Downloads

0

Version

0.2.3

License

none

Last publish

Collaborators

  • dmamills