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

gdate

v1.1.5

Published

A lite extension to the native `Date` object

Downloads

8

Readme

GDATE, Mate!

GDate (pronounced g'date, if you like) is a light-weight extension to the native Date object. Date has a surprising amount of functionality built in, but equally surprising is the functionality it's lacking. (seriously, checkout the code I wrote.... it's nothing mind-blowing, and feels so intuitive). The biggest barriers to using JavaScript's Date are un-intuitive time measurement and a lack of easy date-math functionality (like getting a date 5 days from now or checking if a time falls in a certain range).

GDate makes these much easier by adding some nice layers of abstraction and syntactic sugar, so now you don't need to know how many milliseconds are in a week to get a date 7 days from now!

GDate was written as an extension to it that you can import into whatever project you like as easily as possible. It is tested (see the linked github), and open-source? still in development? If anyone would like easy open-source credits to put on a resumé, or have some ideas how to make the package more valuable or useful feel welcome to add an issue!

Importing it into a project

Import it into your project directory from npm with the usual installation commands in terminal i.e. npm install gdate.

Import it into one of your ES5 files with const gdate = require('gdate'). Import it into one of your ES6 files with import gdate from 'gdate'.

Features:

  • Built-in time properties for easy reference.
    • second: equals 1000ms
    • minute: equals 60s or 60000ms
    • hour: equals 60m or 3600s or 360000ms
    • day: equals 24h or 1440m or 86400s
    • week: equals 7d
    • month: these are set at 30 days for estimation
    • year: equals 365d

Each of these stores the equivalent amount of milliseconds as a number (which is what Date uses under the hood). You can use them like this:

gdate.second // 1000
gdate.day // 8640000

Or, if you're doing a lot of time math, you can use destructuring to make them easier use (this is what will be used for all of the following examples) :

 const { second, minute, hour, day, week, month, year } = gdate;

 console.log( minute ) // 60000
  • advance( Date ).by( distance ): Get new Date instances a set amount of time away. Positive values get future Dates, negative values get past Dates.

    
      const now = new Date();
      const tomorrow = gdate.advance( now ).by( 1 * day );
      const yesterday = gdate.advance().by( -1 * day );

If the Date argument is omitted, it defaults to the current moment. If the distance argument is omitted, it returns a shallow copy of whatever Date was passed in.

  • get( unit ).between( ref1, ref2 ): Get the number of units between 2 dates (order agnostic).
  const now = new Date();
  const tomorrow = gdate.advance( now ).by( 1 * gdate.day );

  gdate.get( hour ).between( now, tomorrow ); // 24
  gdate.get( hour ).between( tomorrow ); // 24

If the ref2 argument is omitted, it defaults to the current moment. If the unit argument is omitted, it defaults to milliseconds.

  • getWhole( unit ).between( ref1, ref2 ): returns the whole number of units that a given number of milliseconds corresponds to, rounded down (order agnostic).
  const now = new Date();
  const ninetyDaysFromNow = gdate.advance( now ).by( 90 * gdate.day );

  gdate.getWhole( week ).between( now, ninetyDaysFromNow ) // 12

If the ref2 argument is omitted, it defaults to the current moment. If the unit argument is omitted, it defaults to milliseconds.

  • is( Date ).between( ref1, ref2): returns true if a given Date is between two reference dates, inclusively and order agnostically. Otherwise, false
  const now = new Date();
  const hourFromNow = gdate.advance( now ).by(1 * gdate.hour );
  const tomorrow = gdate.advance( now ).by( 1 * gdate.day );

  gdate.is( hourFromNow ).between( now, tomorrow ) // true
  gdate.is( hourFromNow ).between( tomorrow ) // true
  gdate.is( now ).between( tomorrow, hourFromNow ) // false
  gdate.is().between( tomorrow, hourFromNow ) // false

If the ref2 or the Date arguments are omitted, they default to the current moment.

  • is( Date ).before( ref ): return true if a date is strictly before a reference. else false
  const now = new Date();
  const tomorrow = gdate.advance( now ).by( 1 * gdate.day );

  gdate.is( now ).before( tomorrow ) // true
  gdate.is( tomorrow ).before( now ) // false

If the ref or the Date arguments are omitted, they default to the current moment.

  • is( Date ).after( ref ): return true if a date is strictly after a reference. else false
  const now = new Date();
  const tomorrow = gdate.advance( now ).by( 1 * gdate.day );

  gdate.is( now ).after( tomorrow ) // false
  gdate.is( tomorrow ).after( now ) // true

If the ref or the Date arguments are omitted, they default to the current moment.

  • createYYYYMMDD( Date ): converts the date into a string of the form YYYY/MM/DD
  const epoch = new Date(0)

  gdate.createYYYYMMDD(epoch) // "1970/01/01"

If the Date argument is omitted, it defaults to the current moment.

getRelativeDistance( Date1, Date2 ): Returns a string of the distance between 2 Dates in the largest whole unit of time (with correct pluralization). If 2 arguments are provided, order is agnostic. If the second argument is excluded, it creates a timestamp from the current moment. Great for Reddit-Style Time-stamps.

  const now = new Date();
  const tomorrow = gdate.advance( now ).by( 1 * day );
  const threeYearsFromNow = gdate.advanceDateBy(1100 * day  , now);

  gdate.getRelativeDistance(now, tomorrow) // "1 day"
  gdate.getRelativeDistance(year) // "3 years"

If the Date2 argument is omitted, it defaults to the current moment.