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-until

v1.0.0

Published

Compare two dates and report the distance in user friendly years, months, weeks, days, hours, and seconds.

Downloads

300

Readme

Time Until

A single purpose javascript module that compares two dates and reports the distance in user friendly years, months, weeks, days, hours, and seconds.

Interface

timeUntil(date, context)

  • date: the input date, as a valid javascript Date object
  • context: a comparison date, defaults to the current time using new Date()

Basic usage:

const { unit, value, string, past } = timeUntil(date)

console.log('Unit', unit) // /(now)|((second|minute|hour|day|week|month|year)s?)/
console.log('Value', value) // /(d+)/
console.log('String', string) // 10 weeks ago, 5 days time, 2 hours, 1 minute ago, now
console.log('Past', past) // true|false

Calling timeUntil(date) returns an object; the .string property can be displayed straight the user, but if you want the break down, then the .unit, .value, and .past / future status is available as well.

Sample output

If assuming now as being Sun Feb 12 2017 23:08:56 GMT:

Time Until
  No change
    √ should interpret 'Sun, 12 Feb 2017 23:08:56 GMT' as now
  Seconds
    √ should interpret 'Sun, 12 Feb 2017 23:09:01 GMT' as 5 seconds
    √ should interpret 'Sun, 12 Feb 2017 23:09:58 GMT' as 62 seconds
  Minutes
    √ should interpret 'Sun, 12 Feb 2017 23:13:56 GMT' as 5 minutes
    √ should interpret 'Sun, 13 Feb 2017 01:07:56 GMT' as 119 minutes
  Hours
    √ should interpret 'Sun, 13 Feb 2017 04:08:56 GMT' as 5 hours
    √ should interpret 'Sun, 14 Feb 2017 22:08:56 GMT' as 47 hours
  Days
    √ should interpret 'Sun, 17 Feb 2017 23:08:56 GMT' as 5 days
    √ should interpret 'Sun, 20 Feb 2017 23:08:56 GMT' as 8 days
    √ should interpret 'Sun, 04 Mar 2017 23:08:56 GMT' as 20 days
  Weeks
    √ should interpret 'Sun, 05 Mar 2017 23:08:56 GMT' as 3 weeks
    √ should interpret 'Sun, 12 Mar 2017 23:08:56 GMT' as 4 weeks
    √ should interpret 'Sun, 22 Mar 2017 23:08:56 GMT' as 5 weeks
    √ should interpret 'Sun, 12 Apr 2017 23:08:56 GMT' as 8 weeks
  Months
    √ should interpret 'Sun, 22 May 2017 23:08:56 GMT' as 3 months
    √ should interpret 'Sun, 12 Jul 2017 23:08:56 GMT' as 5 months
    √ should interpret 'Sun, 12 Feb 2018 23:08:56 GMT' as 12 months
    √ should interpret 'Sun, 12 Jan 2019 23:08:56 GMT' as 23 months
    √ should interpret 'Sun, 12 Feb 2019 23:08:56 GMT' as 24 months
  Years
    √ should interpret 'Sun, 13 Feb 2019 23:08:56 GMT' as 2 years
    √ should interpret 'Sun, 13 Feb 2022 23:08:56 GMT' as 5 years

Usage

As a node module, use npm install time-until to bring the module into your repository, and then require the module into your code:

const timeUntil = require('time-until')

const dateToCheck = Date.parse('Sun, 17 Feb 2017 23:08:56 GMT')
console.log(timeUntil(dateToCheck), 'until', dateToCheck)

For use on a web page, include time-until.js into your project using a script tag, for example:

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="js/time-until.js"></script>
  </head>
  <body>
  ...
  <script>
    const dateToCheck = Date.parse('Sun, 17 Feb 2017 23:08:56 GMT')
    document.write(window.timeUntil(dateToCheck) + ' until ' + dateToCheck)
  </script>
  </body>
</html>

Development

This project uses nodejs and mocha for testing. The project uses standard for linting.

To run the tests locally, checkout this repo using git, then run:

npm install
npm test