npm package
tinydate review
Use tinydate for fixed numeric labels when you control the templates and can validate Date inputs.
tinydate's built-in tokens read local time. Adding a literal Z can therefore mislabel their output as UTC. tinydate 1.3.0 turns a date template into a reusable formatting function. Pass it a native JavaScript Date to combine numeric date fields with your surrounding text. It suits fixed labels such as local build times or filename dates. Your application remains responsible for input validation and timezone policy.
Use tinydate for fixed numeric labels when you control the templates and can validate Date inputs. Before adopting it for timestamps, decide whether local getters meet your requirements or whether maintaining custom UTC callbacks adds unnecessary work.
Answers from our run
tinydate or date-fns: which should you use?
date-fns: Consider date-fns when you need modular date operations using native Date values. Its pinned README describes modular functions and use of the existing native type. The supplied existing URL is the date-fns guide. Use tinydate for fixed numeric labels when you control the templates and can validate Date inputs.
When should you not use tinydate?
You need parsing or calendar arithmetic in the same API.
Use it if
Skip it if
Setup reality
Install the discussed release with npm install tinydate@1.3.0. The examples use the README's CommonJS form, const tinydate = require('tinydate'). The 1.3.0 npm metadata declares main, module, and types entry points. Those declarations do not establish compatibility with your application build. This guide uses the supplied pinned README, tagged implementation, registry metadata, and release-note extracts. No published package archive was inspected for this correction. There is no matching execution measurement: the snippets and expected outputs are source-derived, unexecuted illustrations.
Decide what timezone the output should represent before choosing a template. The built-in map uses local Date getters, including getFullYear, getMonth, getDate, and getHours. There is no timezone argument in the inspected factory. A literal Z only appends text. The UTC example supplies a custom callback for every timestamp field so its date and time components follow the same policy.
Tokens are case-sensitive: MM selects the month, mm selects minutes, and fff selects milliseconds. Creating a formatter stores text and field callbacks for later rendering. Reuse the returned function when the layout repeats. An unsupported token can fail during rendering when its default callback attempts to call a missing map entry. Keep templates under application control and check their allowed fields.
Custom dictionary entries must be functions and must format their own results. Built-in callbacks prepend '00' and slice to the token width. For ordinary dates, that makes a millisecond value of 7 become 007; slicing also truncates years longer than four characters. Validate inputs before formatting. The returned function uses arg || new Date(), so omitted and falsy arguments select the current time. Pass new Date(0), rather than numeric zero, to represent the Unix epoch.
Displayed scores follow the site's published rubric computed in code. The dimension notes describe the editor's evidence assessment of this release: its callable contract, documentation, recorded maintenance work, and declared integration surfaces. They do not establish adoption, current maintainer responsiveness, future compatibility, or tested runtime support.
Patterns
Reuse a local-date formatter reuse-local-date
const tinydate = require('tinydate');
const stamp = tinydate('{YYYY}-{MM}-{DD}');
stamp(new Date(2026, 0, 2, 12)); // Expected: '2026-01-02'
stamp(new Date(2026, 0, 3, 12)); // Expected: '2026-01-03'Unexecuted, source-derived illustration for 1.3.0. The numeric Date constructor uses a zero-based month. This template reads local fields and includes no timezone identifier.
Distinguish minutes from months include-milliseconds
const tinydate = require('tinydate');
const clock = tinydate('{HH}:{mm}:{ss}.{fff}');
clock(new Date(2026, 0, 2, 4, 5, 6, 7));
// Expected: '04:05:06.007'Unexecuted illustration. MM means month; mm means minutes. The built-in callback prepends '00' and slices to the token width.
Request the current local time render-current-time
const tinydate = require('tinydate');
const clock = tinydate('Local time: {HH}:{mm}:{ss}');
const label = clock();
// Output depends on the current local time.Unexecuted illustration. The implementation uses arg || new Date(), so falsy inputs also select the current time. Numeric zero does not select the Unix epoch.
Override a built-in token override-day-padding
const tinydate = require('tinydate');
const stamp = tinydate('{YYYY}-{MM}-{DD}', {
DD: date => date.getDate(),
});
stamp(new Date(2026, 0, 2, 12));
// Expected: '2026-01-2'Unexecuted illustration. The custom DD callback replaces the default callback. Its result receives no automatic padding.
Add a weekday label add-weekday-token
const tinydate = require('tinydate');
const weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const stamp = tinydate('{weekday} {YYYY}-{MM}-{DD}', {
weekday: date => weekdays[date.getDay()],
});
stamp(new Date(2026, 0, 4, 12));
// Expected: 'Sun 2026-01-04'Unexecuted illustration. These English labels belong to the application. They do not demonstrate a built-in locale option.
Supply UTC callbacks for every field override-utc-fields
const tinydate = require('tinydate');
const pad = (value, width) => String(value).padStart(width, '0');
const utc = tinydate('{YYYY}-{MM}-{DD}T{HH}:{mm}:{ss}.{fff}Z', {
YYYY: d => pad(d.getUTCFullYear(), 4),
MM: d => pad(d.getUTCMonth() + 1, 2),
DD: d => pad(d.getUTCDate(), 2),
HH: d => pad(d.getUTCHours(), 2),
mm: d => pad(d.getUTCMinutes(), 2),
ss: d => pad(d.getUTCSeconds(), 2),
fff: d => pad(d.getUTCMilliseconds(), 3),
});
utc(new Date('2026-01-02T04:05:06.007Z'));
// Expected: '2026-01-02T04:05:06.007Z'Unexecuted application code. This layout assumes ordinary four-digit positive years; it is not a complete ISO serializer. Check String.padStart availability in your runtime separately from tinydate's declared support.
Validate Date inputs in a wrapper validate-date-boundary
const tinydate = require('tinydate');
const stamp = tinydate('{YYYY}-{MM}-{DD}');
function formatDate(date) {
if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
throw new TypeError('Expected a valid Date');
}
return stamp(date);
}
formatDate(new Date(2026, 0, 2, 12));
// Expected: '2026-01-02'Unexecuted application code. The wrapper adds validation before calling tinydate. Its instanceof check assumes Date objects from the same JavaScript realm.
Check unsupported tokens during development spot-unsupported-token
const tinydate = require('tinydate');
const stamp = tinydate('{monthName} {DD}');
try {
stamp(new Date(2026, 0, 2, 12));
} catch (error) {
// Expected: TypeError because monthName has no callback.
}
const fixed = tinydate('{monthName} {DD}', {
monthName: () => 'Jan',
});
fixed(new Date(2026, 0, 2, 12));
// Expected: 'Jan 02'Unexecuted illustration. This missing token fails when the formatter renders. The replacement is constant only for this January example; a general callback must select a month label from the supplied Date.
Combine repeated fields with literal text repeat-fields-with-text
const tinydate = require('tinydate');
const stamp = tinydate('Built [{YYYY}-{MM}-{DD}] / year {YYYY}');
stamp(new Date(2026, 0, 2, 12));
// Expected: 'Built [2026-01-02] / year 2026'Unexecuted illustration. Square brackets remain literal text in this layout. The example does not establish an escape syntax for arbitrary brace expressions.
Alternatives
| Package | Registry | Pick it when |
|---|---|---|
| date-fns | npm | Consider date-fns when you need modular date operations using native Date values. Its pinned README describes modular functions and use of the existing native type. The supplied existing URL is the [date-fns guide](https://mrkeyoor.com/libs/date-fns/). |
| luxon | npm | Consider Luxon when you need DateTime, Duration, and Interval types with native time zone and Intl support. Those features are documented in its pinned README. The supplied existing URL is the [Luxon guide](https://mrkeyoor.com/libs/luxon/). |
More web backend guides
How this guide is made: grounded in the library's documentation, release notes, changelog, and issue history, on a fixed rubric — not a hands-on install of every release. The 50 most-downloaded entries are additionally install-verified in clean containers. Corrections: contact the desk.