What is tinydate?
tinydate is a small and efficient date formatting library for JavaScript. It allows you to format dates using a simple and intuitive syntax.
What are tinydate's main functionalities?
Basic Date Formatting
This feature allows you to format dates using a template string. The placeholders {YYYY}, {MM}, and {DD} are replaced with the corresponding parts of the date.
const tinydate = require('tinydate');
const format = tinydate('{YYYY}-{MM}-{DD}');
console.log(format(new Date())); // Outputs: 2023-10-05
Custom Date Formatting
This feature allows you to create custom date formats by combining different placeholders. You can format the time and date in a more readable way.
const tinydate = require('tinydate');
const format = tinydate('{HH}:{mm}:{ss} on {MMMM} {DD}, {YYYY}');
console.log(format(new Date())); // Outputs: 14:30:00 on October 05, 2023
Short Date Formatting
This feature allows you to format dates in a short format using two-digit year, month, and day placeholders.
const tinydate = require('tinydate');
const format = tinydate('{M}/{D}/{YY}');
console.log(format(new Date())); // Outputs: 10/5/23
Other packages similar to tinydate
date-fns
date-fns is a comprehensive date utility library that provides a wide range of functions for manipulating and formatting dates. It is larger in size compared to tinydate but offers more features and flexibility.
moment
Moment.js is a widely-used date manipulation library that provides extensive functionality for parsing, validating, manipulating, and formatting dates. It is more feature-rich but also larger in size compared to tinydate.
dayjs
Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times. It is similar to Moment.js but much smaller in size, making it a good alternative to tinydate for lightweight date formatting.
tinydate
A tiny (349B) reusable date formatter. Extremely fast!
Demo
Inspired by tinytime
, this module returns a "render" function that efficiently re-render your deconstructed template. This allows for incredibly performant results!
However, please notice that this only provides a limited subset of Date methods.
If you need more, tinytime
or date-fns
are great alternatives!
Install
$ npm install --save tinydate
Usage
const tinydate = require('tinydate');
const fooDate = new Date('5/1/2017, 4:30:09 PM');
const stamp = tinydate('Current time: [{HH}:{mm}:{ss}]');
stamp(fooDate);
stamp();
API
tinydate(pattern, dict?)(date?)
Returns: Function
Returns a rendering function that will optionally accept a date
value as its only argument.
pattern
Type: String
Required: true
The template pattern to be parsed.
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.
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');
const stamp = tinydate('Today is: {MMMM} {DD}, {YYYY}', {
MMMM: d => d.toLocaleString('default', { month: 'long' }),
DD: d => d.getDate()
});
stamp(today);
date
Type: Date
Default: new Date()
The date from which to retrieve values. Defaults to current datetime if no value is provided.
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
Benchmarks
# Node v10.13.0
tinydate x 160,834,214 ops/sec ±0.21% (96 runs sampled)
tinytime x 44,602,162 ops/sec ±0.34% (97 runs sampled)
time-stamp x 888,153 ops/sec ±1.27% (86 runs sampled)
License
MIT © Luke Edwards