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

@bedrockio/chrono

v0.3.0

Published

Minimal library for working with dates.

Downloads

92

Readme

Chrono

Chrono is a minimal library to simplify working with dates in Javascript. It has built-in locale and timezone support.

Concepts

Installation

yarn install @bedrockio/yada

DateTime

The DateTime object maintains basic parity with the built-in Date object, however is immutable and has a number of additional methods:

import { DateTime } from '@bedrockio/chrono';

const now = new DateTime();

// Unlike JS dates, the "set" methods return
// a new instance and do not mutate the date.
const january = now.setMonth(0);

Timezones and locales can be set on each instance or globally:

// Sets the timezone for this DateTime
const dt = new DateTime({
  timeZone: 'America/New_York',
  locale: 'en-US',
});

DateTime.setTimeZone('America/New_York');
DateTime.setLocale('en-US');

Date Formatting

The format method accepts two styles of input.

Locale Formatting

Passing an object will use Intl.DateTimeFormat and format a human readable string in a specific locale. It may be a custom object or defined presets. The locale used in order will be:

  • Passed in the options to format
  • The internal locale of the DateTime
  • The globally set locale
  • The system locale
const dt = new DateTime('2020-01-01T00:00:00.000Z');

// December 31, 2019 at 7:00pm
dt.format(DateTime.DATETIME_MED);

// January 1, 2020
dt.format({
  year: 'numeric',
  month: 'long',
  day: 'numeric',
});

Supported presets are:

| Preset | Example | | -------------------- | --------------------------------- | | DATE_MED | January 1, 2020 | | DATE_SHORT | Jan 1, 2020 | | DATE_NARROW | 1/1/2020 | | DATE_MED_WEEKDAY | Wednesday, January 1, 2020 | | TIME_MED | 9:00am | | TIME_SHORT | 9:00a | | TIME_HOUR | 9pm | | TIME_SHORT_HOUR | 9p | | TIME_WITH_ZONE | 9:00am Japan Standard Time | | DATETIME_MED | January 1, 2020 9:00pm | | DATETIME_SHORT | Jan 1, 2020 9:00pm | | DATETIME_NARROW | 1/1/2020 9:00pm | | DATETIME_MED_WEEKDAY | Wednesday, January 1, 2020 9:00pm | | MONTH_YEAR | January 2020 | | MONTH_YEAR_SHORT | Jan 2020 |

Token Formatting

Passing a string will format the date using tokens and ignoring locales.

const dt = new DateTime('2020-01-01T00:00:00.000Z');

// 5:05 am
dt.format('h:mm a');

// 1/1/2020
dt.format('M/d/yyyy');

Supported tokens are:

| Token | Description | | ----- | -------------------------------------------- | | yy | two-digit year | | yyyy | four to six digit year | | M | unpadded month | | MM | padded month | | d | unpadded day of the month | | dd | padded day of the month | | h | unpadded hour in 12-hour time | | hh | padded hour in 12-hour time | | H | unpadded hour in 24-hour time | | HH | padded hour in 24-hour time | | m | unpadded minute | | mm | padded minute | | s | unpadded second | | ss | padded second | | a | lowercase meridiem (am/pm) | | A | uppercase meridiem (AM/PM) | | Z | narrow timezone offset (+5) | | ZZ | short timezone offset (+5000) | | ZZZ | long timezone offset (+5:00) | | ZZZZ | short timezone name (EST) | | ZZZZZ | long timezone name (Eastern Standard Time) |

Interval

The Interval object represents an interval of time:

import { DateTime, Interval } from '@bedrockio/chrono';

const lastYear = new DateTime().rewind(1, 'year');
const nextYear = new DateTime().advance(1, 'year');

const interval = new Interval(lastYear, nextYear);

// The number of days in this interval.
const days = interval.getDays();