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

time-interval-js

v2.1.1

Published

A simple Time Interval class developed for working with javascript date objects and relationships between them.

Downloads

26

Readme

Time Interval JS

npm version

A simple Time Interval class developed for working with javascript date objects and relationships between them.

This package is written in Typescript, therefore there is no additional typing package needed to work with TS.

Getting Started

Install the package with NPM or yarn

npm i time-interval-js
-or-
yarn add time-interval-js

To import and use in your code:

import { TimeInterval } from 'time-interval-js'

And that's it.

Usage

Named Constructors

Time Units:

const interval = TimeInterval.forOneHour();

interval.inHours(1); // 1

Comparison Based Constructor:

const date1 = new Date(2020-01-01);
const date2 = new Date(2020-01-02);

const intervalBetweenTwoDates = TimeInterval.fromTimeBetweenTwoDates(date1, date2);
// Produces a time interval equal to precisely the time between these two dates.

Get a time interval in terms of any common time measurement:

const interval = TimeInterval.forSpecifiedDays(2);

interval.inHours();  // 48
interval.inDays();  // 2
interval.inWeeks();  // 0.2857142857142857
// inHours()
// inDays()
// inSeconds()
// inMilliseconds()

Note: There is a getInApproximateYears method, but it is named this way because there is no guaranteed perfect precision. As we all know, years are not always 365 days, although the package uses this measure to calculate years.

Time Interval Comparison

const daysInterval = TimeInterval.fromSpecifiedDays(5);
const weeksInterval = TimeInterval.fromSpecifiedWeeks(2);

weeksInterval.isLongerThan(daysInterval) // true
daysInterval.isShorterThan(weeksInterval) // true

Adding Time Intervals to Dates

Want to calculate a date that is 5 weeks from now?

const now  = new Date();
const interval = TimeInterval.forSpecifiedWeeks(5);

interval.addToDate(now) // produces a new date that is exactly 5 weeks from the provided date.

Detailed Method Documentation

The code is straightforward enough that you should be able to understand everything by looking at the single source file.

Check it out here.

Interfaces

There is also a TimeIntervalInterface available if you want to mock the object for any reason. The TimeInterval class implements this interface.

import { TimeIntervalInterface } from 'time-interval-js';

Important Note Regarding Time Relationships

TL;DR: All Intervals are positive values.

This library operates on absolute time intervals. Theoretically speaking, a time interval has no negative or positive value, as it is only a relationship between two dates in time, and taking a stance from which point in time to view the difference (as positive or negative) is an additional property which the package currently does not support. Maybe later :)

Therefore, don't expect to get a negative value when you create a time interval between today and last week.

const lastWeek = new Date('2020-01-05');
const today = new Date('2020-01-12');

const interval = TimeInterval.fromTimeBetweenTwoDates(lastWeek, today);

interval.inSeconds()  // will ALWAYS be positive

All time intervals will have positive values. In the future perhaps we can add some specific methods which allow us to consider a perspective from which to judge the relationship between two points in time.