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

timespan.cs

v1.0.0

Published

A C#-esque TimeSpan object for Node which makes differential time operations easy

Downloads

76

Readme

TimeSpan

A CSharp-esque TimeSpan object for JavaScript

Anyone who's used C# knows that it has some of the best native libraries on the planet - kudos to Microsoft. Of those, the ones I always find myself wishing for when writing JS code are C#'s TimeSpan and Date structures - so here's the TimeSpan object rewritten for JavaScript developers who want a lightweight, powerful and portable way to play with timespans.

Example

var TimeSpan = require('timespan.cs');
var ts = new TimeSpan(
	5 /*days*/, 
	2 /*hours*/, 
	12 /*minutes*/, 
	7 /*seconds*/, 
	0 /*milliseconds*/);

console.log('%s days', ts.toString('TD'));
// -> 5.091747685185185

Creating a TimeSpan

TimeSpans are created one of two ways, either by calling the timespan constructor or by using Date.subtract. Using the constructor allows you to specify the number of milliseconds, seconds, minutes, hours and days (or a subset thereof) contained within the timespan.

new TimeSpan(milliseconds);
new TimeSpan(seconds, milliseconds);
new TimeSpan(minutes, seconds, milliseconds);
new TimeSpan(hours, minutes, seconds, milliseconds);
new TimeSpan(days, hours, minutes, seconds, milliseconds);

Date.subtract(anotherDate);
Date.subtract(milliseconds);
Date.subtract(seconds, milliseconds);
Date.subtract(minutes, seconds, milliseconds);
Date.subtract(hours, minutes, seconds, milliseconds);
Date.subtract(days, hours, minutes, seconds, milliseconds);

Using a TimeSpan

Each timespan makes a number of properties available for you to access, these are split into totalXXX and XXX properties. Total properties return the total number of [milliseconds, seconds, minutes, hours, days, months, years] that are contained within the TimeSpan. These values are Real Numbers, meaning that they have a decimal component representing partial values.

Similarly, the XXX properties represent the number of full [milliseconds, seconds, minutes, hours, days, months, years] within the TimeSpan should all other full values be used. Sounds complicated, it's not - a timespan with 25 hours in it will have 1 day and 1 hour with 0 minutes, 0 seconds and 0 milliseconds.

ts.totalMilliseconds;
ts.totalSeconds;
ts.totalMinutes;
ts.totalHours;
ts.totalMonths;
ts.totalYears;

ts.milliseconds;
ts.seconds;
ts.minutes;
ts.hours;
ts.months;
ts.years;

Month and Year Components

It is important to understand that a TimeSpan represents a vector in time - it has magnitude (the amount of time within it) and direction (whether it's forwards or backwards in time) but has no point of origin. Because of the way the Gregorian calendar works (with its funky 28, 30 and 31 days in months and 365/366 days in a year) it is impossible to accurately determine the exact number of days that are contained within a TimeSpan - so what this library does is use the standard 30.4375 days in a month (365 * 3 + 366) / 48 to determine the number of months and years that have passed within a specific TimeSpan. It is important to keep this in mind when working with months and years.

Comparing TimeSpans

TimeSpans can be compared using .equals(), .shorterThan() and .longerThan(), allowing you to easily check how one timespan relates to another. Similarly to the way Date.subtract works, you can specify any subset of days, hours, minutes, seconds and milliseconds as arguments instead of a TimeStamp object.

ts1.equals(ts2);
ts1.equals(milliseconds);
ts1.equals(seconds, milliseconds);
// And so on...

ts1.shorterThan(ts2);
ts1.shorterThan(milliseconds);
ts1.shorterThan(seconds, milliseconds);
// And so on...

ts1.longerThan(ts2);
ts1.longerThan(milliseconds);
ts1.longerThan(seconds, milliseconds);
// And so on...