A tiny library that converts milliseconds to and from CSS time.


A tiny JavaScript library that converts milliseconds to and from CSS time.

You can install css-time.js with one of the package managers: NPM; Jam; Bower; or Component. The package name for the first three is css-time and for Component it's philbooth/css-time.js:

npm install css-time
jam install css-time
bower install css-time
component install philbooth/css-time.js

Alternatively, you can just clone the git repository from GitHub:

git clone git@github.com:philbooth/css-time.js.git

Both CommonJS (e.g. if you're using Browserify) and AMD (e.g. if you're using Require.js) loading styles are supported. If neither system is detected, the library defaults to exporting it's interface globally as cssTime.

Two functions are exported, from and to.

Returns the number of milliseconds represented by a CSS time string.

cssTime.from('500ms'); // returns 500
cssTime.from('-1.5s'); // returns -1500

Returns a CSS time string representing the number of milliseconds passed in the argument.

cssTime.to(500); // returns '500ms'
cssTime.to(-0.5); // returns '-0.5ms'

The build environment relies on Node.js, NPM, Jake, JSHint, Mocha, Chai and UglifyJS. Assuming that you already have Node.js and NPM set up, you just need to run npm install to install all of the dependencies as listed in package.json.

The unit tests are in test/css-time.js. You can run them with the command npm test or jake test. To run the tests in a web browser, open test/css-time.html.