tiny_date_formatter
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

Tiny Date Formatter

logo

npm size-badge Types


A tiny (~350B) reusable date formatter. Extremely fast!

📝 Table of Contents

🧐 About

An update of tinydate by Luke Edwards, this module returns a "render" function that efficiently re-render your deconstructed template. This allows for highly performant results!

However, please notice that this only provides a limited subset of Date methods.

Install

Package Manager

NPM

npm i tiny_date_formatter

PNPM

pnpm add tiny_date_formatter

Yarn

yarn add tiny_date_formatter

CDN

Skypack

For Web and Deno, no install is required! Just put this line at the top of your file:

import { formatter } from 'https://cdn.skypack.dev/tiny_date_formatter';

If you want type support with skypack, follow the directions here

UNPKG

<script src="https://unpkg.com/tiny_date_formatter"></script>

And use it like you would any other package from UNPKG

🔧 Running the tests

The basic set of tests are in the test script, the coverage script, and the report script. Just run those using your perferred package manager (npm, yarn, pnpm, etc.) to make sure nothing has broken.

🎈 Usage

Here's the great part: thanks to microbundle, this package supports CJS, UMD, and ESM formats. That means that wherever and however you use this package — in browser or node, with import or require — you should be set, no configuration required.

Example

import { formatterFactory } from ('tiny_date_formatter');
const exampleDate = new Date('5/1/2017, 4:30:09 PM');

const stamp = formatterFactory('Current time: [{HH}:{mm}:{ss}]');

stamp(fooDate);
//=> Current time: [16:30:09]

stamp(new Date());
//=> Current time: [17:09:34]

API

formatterFactory(pattern, dict?)(date?)

Returns: Function(Date)=>string

Returns a rendering function that accepts a date value as its only argument.

pattern

Type: String Required: true

The template pattern to be parsed. any characters besides braces are allowed. A TypeError will be thrown if the text inside doesn't match any of the defaults or newly entered custom patterns. Examples: "{HH}:{mm}" "Last changed {MM}"

dict

Type: Object Required: false

A custom dictionary of template patterns. You may override existing patterns or declare new ones.

Important: All dictionary items must be a function and must control its own formatting. A date will be passed in but it is not necessary to use it. For example, when defining your own {ss} template, tinydate will not pad its value to two digits.

const today = new Date('2019-07-04, 5:30:00 PM');

// Example custom dictionary:
//   - Adds {MMMM}
//   - Overrides {DD}
const stamp = tinydate('Today is: {MMMM} {DD}, {YYYY}', {
 MMMM: d => d.toLocaleString('default', { month: 'long' }),
 DD: d => d.getDate()
});

stamp(new Date());
//=> 'Today is: July 4, 2019'

Other example dictionaries: {SP: ()=>"__spaces__"}, {'DD-3':(d:Date)=>d.getDate()-3.toString()}

Patterns

  • {YYYY}: full year; eg: 2017
  • {YY}: short year; eg: 17
  • {MM}: month; eg: 04
  • {DD}: day; eg: 01
  • {HH}: hours; eg: 06 (24h)
  • {mm}: minutes; eg: 59
  • {ss}: seconds; eg: 09
  • {fff}: milliseconds; eg: 555

📃 License

Distributed under the MIT License. See LICENSE for more information.

✍️ Authors

Find me @illumincrotty on github or @illumincrotty on twitter

🔨 Similar Tools

If this tool isn't working for you, try one of these:

  • date-fns
  • @bitty/format-date
  • fecha

Package Sidebar

Install

npm i tiny_date_formatter

Weekly Downloads

5

Version

2.0.1

License

MIT

Unpacked Size

37.3 kB

Total Files

13

Last publish

Collaborators

  • illumincrotty