A chainable time span object to use with native js dates
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bin
tests
.gitignore
.travis.yml
GruntFile.js
Makefile
README.md
index.js
package.json
timespan.js

README.md

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);