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

@planningcenter/datetime-fmt

v1.3.6

Published

Consistent formatting of dates on Church Center

Downloads

2,436

Readme

@planningcenter/datetime-fmt

@planningcenter/datetime-fmt offers consistent formatting of dates and times. It is built to format dates, times, and dates-with-times given a timestamp or a range of timestamps. Special care is given to dropping redundant date information in ranges.

Usage

There are three functions.

  • date for formatting dates
  • time for formatting times
  • datetime for formatting a dates-with-times

These functions all have the same signature

    date(start, end = null, configuration = {})
    time(start, end = null, configuration = {})
datetime(start, end = null, configuration = {})
  • start is required. It's is the timestamp that you'd like to format, or the beginning of a time range.
  • end is optional. If present, it represents the end of a time range.
  • configuration is used to deviate from the "standard" formatting.

Configuration

There are a few of configuration options. Most of the time, you won't need these. But if you ever do, it's good to know they exist.

const defaultConfiguration = {
  dateFirst: false,
  hour12: true,
  timeZone: "America/Los_Angeles",
  showTimeZone: 'automatic', // 'automatic'/true/false
  style: "standard",
  year: false,
  yearSeparator: ", ",
  truncateSameMonth: true,
  truncateSameYear: true,
  anchorDate: new Date("2020-08-01T15:16:23Z"), // very optional - only used to anchor relative dates from a date other than `moment()` (current datetime), primarily for tests
}

The most often used configuration options will be year and style.

If year is true, the year is appended to a date.

The valid values for style are:

standard
short
long
abbreviated
abbreviated-long
relative
relative-short

You can see an exhaustive list of how these styles apply to dates in the tests. For brevity, here's the gist of named style to the formatted date it produces.

const dateString = "2020-08-01T15:16:23Z"
expect(datetimeFmt.date(dateString, { style: "standard" })).toEqual("August 1")
expect(datetimeFmt.date(dateString, { style: "short" })).toEqual("8/1")
expect(datetimeFmt.date(dateString, { style: "long" })).toEqual("Saturday, August 1")
expect(datetimeFmt.date(dateString, { style: "abbreviated" })).toEqual("Aug 1")
expect(datetimeFmt.date(dateString, { style: "abbreviated-long" })).toEqual("Sat, Aug 1")
expect(datetimeFmt.date(dateString, { style: "relative", anchorDate: new Date(dateString) })).toEqual("Today")
expect(datetimeFmt.date(dateString, { style: "relative-short", anchorDate: new Date(dateString) })).toEqual("8:16am")

Setting configuration globally

For hour12 and dateFirst in particular, we'll probably want to configure the library globally with the current organization's settings.

The setGlobalConfiguration and resetGlobalConfiguration functions can be used to setup/teardown the global configuration.

What else

This README is intentionally short while we propose this API. The exhaustive tests are a great resource for digging into the finer details.

Running tests

yarn test

Cheat Sheet

The style and year options are the most heavily used. Here's a cheat sheet of how they can be used to format dates.

|Desired format|style option |year option | |-|-|-| |August 1|standard|false| |August 1, 2020|standard|true| |8/1|short|false| |8/1/2020|short|true| |Aug 1|abbreviated|false| |Aug 1, 2020|abbreviated|true| |Saturday, August 1|long|false| |Saturday, August 1, 2020|long|true| |Sat, Aug 1|abbreviated-long|false| |Sat, Aug 1, 2020|abbreviated-long|true|

Relative Dates

Relative dates are a special snowflake, and thus deserve their own cheatsheet.

Note that while they do technically support date ranges, they've been built primarily for displaying single (start) dates - so there might be some extra work needed to display ranges in a usable fashion.

Assuming a current date of Saturday, August 1, 2020 at 8:16am PT:

| | date(date, { style: "relative-short" }) | date(date, { style: "relative" }) | datetime(date, { style: "relative" }) | |---------------------------|-------------|-----------------------|---------------------------------| | More than a year ago | Aug 1, 2019 | August 1, 2019 | August 1, 2019 at 8:16am | | Within the last year | Feb 1 | Saturday, February 1 | Saturday, February 1 at 8:16am | | Within the last week | Wed | Wednesday | Wednesday at 8:16am | | The day before | Fri | Yesterday | Yesterday at 8:16am | | Same day | 8:16am (yes, really) | Today | Today at 8:16am | | The next day | Sun | Tomorrow | Tomorrow at 8:16am | | Within the next week | Tue | Tuesday | Tuesday at 8:16am | | Within the next year | Feb 1 | Monday, February 1 | Monday, February 1 at 8:16am | | More than a year from now | Aug 1, 2022 | August 1, 2022 | August 1, 2022 at 8:16am |

Time Zones

The showTimeZone option determines whether the configured timezone's abbreviation is appended to the time string:

  • If true, the abbreviation is always appended to the time/datetime string (8:16am PDT)
  • If false, the abbreviation is never appended (8:16am)
  • If automatic, the abbreviation is appended to the time/datetime string if and only if the viewer's timezone differs from the configured timezone.