@lukeboyle/timetools

2.0.2 • Public • Published

timetools

Build Status Coverage Status StyleCI

What does timetools do?

timetools makes it easier to deal with times in Javascript. For example, new Date().getTime() at the time of writing this returns 1452734171694.

If you want to know how many days/weeks/years there are in that, your code can get pretty crowded with maths. That's where timetools comes in.

Installation

npm install --save timetools

Usage

format

format accepts a value in milliseconds and converts it to a unit of your choice. Such as years, days, weeks.

A simple example

var timeTools = require('time-tools')

// 1000 ms to seconds
timeTools.format(1000, 'seconds');

// > 1

A slightly more intense example

var millisecondsPerDay = 1000 * 60 * 60 * 24;

timeTools.format(millisecondsPerDay * 3, 'days');

// > 3

Applying these examples

// The current date
var date1 = new Date().getTime();
// The current date minus 3 years
var date2 = new Date().setFullYear(new Date().getFullYear() - 3);

// convert the difference of those two dates into days
timeTools.format(date1 - date2, 'days');

// > 1095

You could also replace 'days' in the last example with 'weeks' or 'years'.

parse

parse takes the number of a unit of time and converts it to milliseconds

A simple example

// 1 day in ms
timeTools.parse(1, 'day');

// > 86400000

Applying this example

var a = new Date().get Time() 

a - timeTools.parse(3, 'years')

// > 1452856052790

This returns the current time, minus 3 years. You could also take off days, weeks, hours etc.

API

Available functions

format

  • Takes a time in milliseconds and returns your chosen unit.

Arguments

Argument Type Default Options
rawTime Number None None
timeUnit String None nanoseconds, microseconds, milliseconds, seconds, minutes, hours, days, weeks, years

parse

  • Takes a number and a unit and returns the value in milliseconds.

Arguments

Argument Type Default Options
number Number None None
timeUnit String None nanoseconds, microseconds, milliseconds, seconds, minutes, hours, days, weeks, years

Running tests

Simply run npm install and run npm test

Package Sidebar

Install

npm i @lukeboyle/timetools

Weekly Downloads

10

Version

2.0.2

License

MIT

Last publish

Collaborators

  • lukeboyle