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

@solid-primitives/date-difference

v1.0.2

Published

Reactive primitives that provide autoupdating, relative date features.

Downloads

5

Readme

@solid-primitives/date-difference

lerna size size

Two reactive primitives that provide autoupdating, relative date features.

Installation

npm install @solid-primitives/date-difference
# or
yarn add @solid-primitives/date-difference

How to use it

createDateNow

Creates an autoupdating and reactive new Date().

import { createDateNow } from "@solid-primitives/date-difference";

// updates every second:
const [now] = createDateNow(1000);

// won't autoupdate:
const [now, update] = createDateNow(0);

// update manually:
update();

The types are:

function createDateNow(interval?: MaybeAccessor<number>): [Accessor<Date>, Function];

createDateDifference

Provides a reactive, formatted date difference in relation to now.

import { createDateDifference } from "@solid-primitives/date-difference";

const [myDate, setMyDate] = createSignal(new Date("Jun 28, 2021"));
const [timeago, { target, now, update }] = createDateDifference(myDate);
// => 5 months ago

// use custom libraries to change formatting:
import { formatRelative } from "date-fns";
const [timeago] = createDateDifference(1577836800000, {
  min: 10000,
  updateInterval: 30000,
  relativeFormatter: (target, now) => formatRelative(target, now)
});
// => last Monday at 9:25 AM

The types are:

function createDateDifference(
  date: MaybeAccessor<number | Date | string>,
  options?: DateDifferenceOptions
): [
  Accessor<string>,
  {
    target: Accessor<Date>;
    now: Accessor<Date>;
    update: Function;
  }
];

interface DateDifferenceOptions {
  /**
   * Intervals to update, set 0 to disable auto update
   *
   * @default (diff) => diff <= 3600_000 ? 30_000 : 1800_000
   */
  updateInterval?: number | ((diff: number) => number);

  /**
   * Minimum diff in milliseconds to display "just now" instead of relative time
   *
   * @default 60000
   */
  min?: number;

  /**
   * Maximum diff in milliseconds to display the full date instead of relative
   *
   * @default Infinite
   */
  max?: number;

  /**
   * Messages for formating the string
   */
  messages?: Partial<DateDifferenceMessages>;

  /**
   * Formatter for full date
   */
  absoluteFormatter?: (date: Date) => string;

  /**
   * Relative time formatter
   */
  relativeFormatter?: RelativeFormatter;
}

type MessageFormatter<T = number> = (value: T, isPast: boolean) => string;

type RelativeFormatter = (
  target: Date,
  now: Date,
  diff: number,
  messages: DateDifferenceMessages
) => string;

interface DateDifferenceMessages {
  justNow: string;
  past: string | MessageFormatter<string>;
  future: string | MessageFormatter<string>;
  year: string | MessageFormatter<number>;
  month: string | MessageFormatter<number>;
  day: string | MessageFormatter<number>;
  week: string | MessageFormatter<number>;
  hour: string | MessageFormatter<number>;
  minute: string | MessageFormatter<number>;
  second: string | MessageFormatter<number>;
}

Demo

https://codesandbox.io/s/solid-date-difference-hjxui?file=/index.tsx

Changelog

0.0.100

Initial release as a Stage-0 primitive.

1.0.0

Stage-2 realease.

1.0.1

Updated build process and documentation.

Acknowledgement