npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lukeboyle/timetools

v2.0.2

Published

Simple tools for dealing with millisecond times in Javascript.

Downloads

9

Readme

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