Date

To manipulate date & time values in different locales

Installation

Import this plugin in your _config.ts file to use it:

import lume from "lume/mod.ts";
import date from "lume/plugins/date.ts";

const site = lume();

site.use(date({/* your config here */}));

export default site;

See all available options in Deno Doc.

Description

This plugin registers the date filter, which allows manipulating and formatting a datetime value in different locales. It uses the date-fns library under the hood.

<time>{{ createdAt | date }}</time>

Formats

By default, the value is formatted to yyyy-MM-dd, but you can use the first argument to set a different format. See the date-fms documentation for more info.

<time>{{ createdAt | date('MM/dd/yyyy') }}</time>

There are some predefined formats that you can use:

Name Format
ATOM yyyy-MM-dd'T'HH:mm:ssxxx
DATE yyyy-MM-dd
DATETIME yyyy-MM-dd HH:mm:ss
TIME HH:mm:ss
HUMAN_DATE PPP
HUMAN_DATETIME PPPppp
<time datetime="{{ createdAt | date }}">
  {{ createdAt | date('HUMAN_DATE') }}
</time>

After installing the plugin you can edit or add more named formats, so it's easier to apply them in templates:

site.use(date({
  formats: {
    "MY_FORMAT": "MM-dd-yyyy",
  },
}));

Locales

date-fns has support for multiple locales. You can configure them in _config.ts:

import date from "lume/plugins/date.ts";

site.use(date({
  locales: ["gl", "es"],
}));

Use the second argument to set the locale:

<time datetime="{{ createdAt | date }}">
  {{ createdAt | date("HUMAN_DATE", "gl") }}
</time>

The first locale set in the _config.js is also used as the default locale.