Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@planningcenter/datetime-fmt

Package Overview
Dependencies
Maintainers
16
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@planningcenter/datetime-fmt

Consistent formatting of dates on Church Center

  • 1.3.6
  • latest
  • npm
  • Socket score

Version published
Maintainers
16
Created
Source

@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 formatstyle optionyear option
August 1standardfalse
August 1, 2020standardtrue
8/1shortfalse
8/1/2020shorttrue
Aug 1abbreviatedfalse
Aug 1, 2020abbreviatedtrue
Saturday, August 1longfalse
Saturday, August 1, 2020longtrue
Sat, Aug 1abbreviated-longfalse
Sat, Aug 1, 2020abbreviated-longtrue

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 agoAug 1, 2019August 1, 2019August 1, 2019 at 8:16am
Within the last yearFeb 1Saturday, February 1Saturday, February 1 at 8:16am
Within the last weekWedWednesdayWednesday at 8:16am
The day beforeFriYesterdayYesterday at 8:16am
Same day8:16am (yes, really)TodayToday at 8:16am
The next daySunTomorrowTomorrow at 8:16am
Within the next weekTueTuesdayTuesday at 8:16am
Within the next yearFeb 1Monday, February 1Monday, February 1 at 8:16am
More than a year from nowAug 1, 2022August 1, 2022August 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.

FAQs

Package last updated on 13 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc