A tiny (~350B) reusable date formatter. Extremely fast!
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.
npm i tiny_date_formatter
pnpm add tiny_date_formatter
yarn add tiny_date_formatter
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
<script src="https://unpkg.com/tiny_date_formatter"></script>
And use it like you would any other package from UNPKG
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.
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.
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]
Returns: Function(Date)=>string
Returns a rendering function that accepts a date
value as its only argument.
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}"
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()}
-
{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
Distributed under the MIT License. See LICENSE
for more information.
Find me @illumincrotty on github or @illumincrotty on twitter
If this tool isn't working for you, try one of these:
- date-fns
- @bitty/format-date
- fecha