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

timizz

v1.0.0

Published

A simple utility to represent, create or substract time intervals using raw time units or Dates

Downloads

3

Readme

timizz

A simple utility to represent, create or substract time intervals using raw time units or Dates.

Timizz is a small library used to represent time periods in the most descriptive way possible.

It can also be used to convert between time units, for example between milliseconds to hours.

Installation

npm i timizz
# or
yarn add timizz

Usage

Import the package using import or require.

import Timizz from 'timizz';
// or
const { Timizz } = require('timizz');

Get a time object

Create a time object with one of the following static methods:

  • ms / milliseconds
  • s / seconds
  • mn / minutes
  • h / hours
  • d / days
  • compose (compose an interval from any of the units up there)
  • from (create a time from an existing Date object)
// Create from an helper unit method
const tenM = Timizz.minutes(10); // or Timizz.mn(10);
const secs = Timizz.s(105); // or Timizz.seconds(105);

// Compose from multiple units
const interval = Timizz.compose({ ms: 250, seconds: 30, hours: 2 });

// Create from a date
const date = Timizz.from(new Date('2020-01-01'));

Use the interval

  • Use .after/.afterNow/.before/.beforeNow to use an interval to create a date.
  • Use .diff to compute an absolute difference between two intervals/dates.
  • Use .add/.sub to add/substract time to an time object.
  • Once you have a "time object", you can use ms/milliseconds/s/seconds/mn/minutes/h/hours/d/days getters to have a representation of the time object into the wanted unit.
// Assume that we have a time object
const time = Timizz.hours(2);

// Convert the interval as a Date object
const asDate = time.date;

// Create a date from an interval
// 1) Create a date 10 minutes after now
// Now is: Sun May 02 2021 20:08:07
Timizz.minutes(10).afterNow; // Sun May 02 2021 20:18:07

// 2) Create a date 15m20s after a specific point of time
Timizz
  .compose({ minutes: 15, seconds: 20 })
  .after(new Date('2020-01-05T15:57:00Z')) // Sun Jan 05 2020 16:12:20

// 3) Create a date 15 hours before now or before a point in time
Timizz.h(15).beforeNow;
Timizz.h(15).before(new Date('2020-01-05T15:57:00Z')); // Sun Jan 05 2020 00:57:00

// 4) Get the absolute difference between times/intevals (in days)
// Note: Timizz.now is a shortcut for Timizz.from(new Date())
Timizz
  .from(new Date('2021-06-28')) // Create from a date
  .diff(Timizz.now) // Get the difference between interval and now
  .days // Get the difference in days

// 5) Add or substract time to a time object
// This create a new time object! Every object is immutable
// 10 seconds minus 5 milliseconds
Timizz
  .seconds(10) // New time object of 10 seconds
  .sub(Timizz.ms(5)) // New time object of 10 seconds - 5 milliseconds
  .ms // Get the substracted time as milliseconds (9995)

// 2 hours plus 34 minutes and 10 seconds
Timizz
  .hours(2) // New time object of 2h
  .add(Timizz.compose({ minutes: 34, seconds: 10 })) // New time object of 2h + 34min10s
  .seconds // Get the total time as seconds (9250)

Convert between units

  • Use the unit getters seen before on a time object to get a representation into the wanted unit.
// 15 hours in seconds
Timizz.hours(15).seconds // 54000

// 20 minutes 32 seconds as days
Timizz.compose({ minutes: 20, seconds: 32 }).days // 0.014259...