Date Plugins
Date plugins allow you to format dates, get unix timestamps, or manipulate dates by adding or removing hours, days, weeks, and more.
The date plugins depend on date-fns
, and you will need to install it
separately from microfern.
npm i @microfern/date
You can then import and use the plugins when calling format.
import { format } from "microfern";
import { DATE_PLUGINS } from "@microfern/date";
// I'll eat another apple on 1719761084
format(
"I'll eat another apple on {{ date | toUnixTimestamp }}",
{ now: new Date().toString() },
{ plugins: DATE_PLUGINS }
// If you want to use both the date plugins and the built-in plugins, you can merge them like this:
// { plugins: { ...DATE_PLUGINS, ...BUILT_IN_PLUGINS } }
);
Date Inputs
The date plugins can accept dates formatted in multiple ways:
- a Unix timestamp (number formatted as a string), e.g.
"1719761084"
- an ISO 8601 formatted date-time string, e.g.
"2023-05-31T09:00:00-05:00"
- a
Date
object formatted with.toString()
e.g."Sun Jun 30 2024 12:39:20 GMT-0500 (Central Daylight Time)"
List of Date Plugins
toUnixTimestamp
This plugin converts a date string to a Unix timestamp (seconds since January 1, 1970).
The avocado will be ripe at timestamp {{ ripeness | toUnixTimestamp }}
{ "ripeness": "2023-06-15T14:30:00Z" }
toISODateTime
This plugin converts a date string to an ISO 8601 formatted date-time string.
The great cucumber harvest of {{ harvest | toISODateTime }}
{ "harvest": "1685541600" }
addTime
This higher-order plugin adds a specified amount of time to a date. It requires two parameters: the amount and the unit of time.
Your artichoke hearts will be ready in {{ cookTime | addTime 30 minutes }}
{ "cookTime": "2023-06-15T12:00:00Z" }
subtractTime
This higher-order plugin subtracts a specified amount of time from a date. It requires two parameters: the amount and the unit of time.
The last thyme I saw you was {{ lastMeeting | subtractTime 2 days }}
{ "lastMeeting": "2023-06-15T09:00:00Z" }
formatDateTime
This higher-order plugin formats a date according to the specified format string. It can take multiple format tokens as separate arguments.
The Annual Turnip Toss will be held on {{ event | formatDateTime MMMM do 'at' h:mm a }}
{ "event": "2023-07-01T14:30:00Z" }
Time Units
When using addTime
and subtractTime
, you can specify the following time units:
- years
- months
- weeks
- days
- hours
- minutes
- seconds
Format Tokens
The formatDateTime
plugin uses format tokens from the date-fns
library. Here are some common tokens:
yyyy
: 4-digit yearMM
: 2-digit monthdd
: 2-digit day of the monthHH
: 2-digit hour (24-hour clock)mm
: 2-digit minutess
: 2-digit secondMMMM
: Full month namedo
: Day of the month with ordinalh
: Hour (12-hour clock)a
: AM/PM indicator
For a full list of format tokens, refer to the date-fns format documentation.