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

datetime-placeholder

v1.1.0

Published

Simplifies a date/time-formatting pattern using Unicode LDML tokens to a pattern usable in date/time pickers.

Downloads

4

Readme

DateTime Pattern Placeholder

Latest version Dependency status Coverage Codacy Badge

Simplifies a date/time-formatting pattern using Unicode LDML tokens to a pattern usable in date/time pickers.

Although Unicode LDML is a widely accepted standard for tokens in date/time formatting patterns, the variety of the tokens is not comprehensible, when presented in a date/time picker to an end-user. Patterns presented as value placeholders can be simpler, because the parser accepts more variants of the input form of a date/value part.

| Locale | Formatting pattern | Picker placeholder | |:-------|:--------------------|:----------------------------| | en | M/d/yy, h:mm a | mm/dd/yy, hh:mm aa | | cs | d. MMMM y H:mm:ss z | dd. mmm yyyy hh:mm:ss zz |

const placeholder = simplifyDateTimePattern('d.M.yy') // dd.mm.yy
  • ES, CJS and UMD module exports.
  • TypeScript type declarations (typings).
  • No other dependencies.
  • Tiny code base - 1.88 kB minified, 801 B gzipped, 731 B brotlied.

Related projects:

Installation

This module can be installed in your project using NPM, PNPM or Yarn. Make sure, that you use Node.js version 16.14 or newer.

$ npm i datetime-placeholder
$ pnpm i datetime-placeholder
$ yarn add datetime-placeholder

Functions are exposed as named exports from ES and CJS modules, for example:

import { simplifyDateTimePattern } from 'datetime-placeholder'
const { simplifyDateTimePattern } = require('datetime-placeholder')

A UMD module can be loaded to the browser either directly:

<script src="https://unpkg.com/[email protected]/lib/index.min.js"></script>
<script>
  const { simplifyDateTimePattern } = window.dateTimePlaceholder
</script>

Or using an AMD module loader:

<script>
  require([
    'https://unpkg.com/[email protected]/lib/index.min.js'
  ], ({ simplifyDateTimePattern }) => {
    ...
  })
</script>

API

simplifyDateTimePattern(pattern, options?)

Returns a simplified pattern to show to the end-user in a date/time picker.

The pattern argument must be a date/time-formatting pattern using Unicode LDML tokens.

import { simplifyDateTimePattern } from 'datetime-placeholder'

const placeholder = simplifyDateTimePattern('M/d/yy')
console.log(placeholder) // prints 'mm/dd/yy'

simplifyDateTimePatternToParts(pattern, options?)

Returns a simplified pattern to show to the end-user in a date/time picker as an array of parts, which values are supposed to be concatenated together.

The pattern argument must be a date/time-formatting pattern using Unicode LDML tokens.

import { simplifyDateTimePatternToParts } from 'datetime-placeholder'

const placeholder = simplifyDateTimePatternToParts('M/d/yy')
console.log(placeholder) // prints the following:
// [{ type: 'month', value: 'mm',
//    type: 'literal', value: '/',
//    type; 'day', value: 'dd',
//    type: 'literal', value: '/',
//    type: 'year': value: 'yy' }]

Options

The pattern simplification can be customised by an optional parameter - an object with the following properties:

| Name | Default | Description | |:-------------|:--------------|:------------| | letterCase | 'lowercase' | if the pattern should be rendered lowercase ('lowercase') or uppercase ('uppercase') |

Pattern Conversion

The characters wrapped between two single quotes characters (') are escaped. Two single quotes in a row, whether inside or outside a quoted sequence, represent a "real" single quote. (see the last example)

The input pattern is based on Unicode Technical Standard #35. Only those tokens are supported, which can appear in patterns for formatting instances of Date by Intl.DateTimeFormat. For example, only the "formatting" variant of a token (M, E) is supported, not the "stand-alone" one (L, i). ("Formatting" means declined according to the rules of the language in the context of a date. "Stand-alone" means always nominative singular.)

Accepted patterns:

| Unit | Pattern | Placeholder | Meaning | |------------------------------|---------|-------------|--------------| | Era | G... | ee | era | | Calendar year | y | yyyy | full year | | | yy | yy | 2-digit year | | | yyy... | yyyy | full year | | Month | M | MM | mm | month number | | | MMM... | mmm | month name | | Day of month | d | dd | dd | day number | | Day of week | E... | www | weekday name | | AM, PM | a... | aa | AM/PM | | AM, PM, noon, midnight | b... | aa | | | Flexible day period | B... | aa | | | Hour [1-12] | h | hh | hh | hours | | Hour [0-23] | H | HH | hh | | | Hour [0-11] | K | KK | hh | | | Hour [1-24] | k | kk | hh | | | Minute | m | mm | mm | minutes | | Second | s | ss | ss | seconds | | Fraction of second | S... | mmm | milliseconds | | Timezone (ISO-8601 w/ Z) | X... | zz | time zone | | Timezone (ISO-8601 w/o Z) | x... | zz | | | Timezone (GMT) | O... | zz | | | Timezone (specific non-loc.) | z... | zz | |

Although some placeholders are the same, their meaning is clear from the context within the pattern, because people know how to write down a date/time value.

Examples

// Represent 11 February 2014 in middle-endian format
simplifyDateTimePattern('MM/dd/yyyy')
//=> 'mm/dd/yyyy'
// Escape string by single quote characters
simplifyDateTimePattern("h 'o''clock'", { letterCase: 'uppercase' })
//=> "HH o'clock"

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

License

Copyright (c) 2023 Ferdinand Prantl

Licensed under the MIT license.