v1.9.1
Date filter is used to convert a timestamp into the specified format.
- LiquidJS tries to conform to Shopify/Liquid, which uses Ruby’s core Time#strftime(string). There’re differences with Ruby’s format flags:
%Z
(since v10.11.1) is replaced by the passed-in timezone name fromLiquidOption
or in-place value (see TimeZone below). If passed-in timezone is an offset number instead of string, it’ll behave like%z
. If there’s none passed-in timezone, it returns the runtime’s default time zone.- LiquidJS provides an additional
%q
flag for date ordinals. e.g.{{ '2023/02/02' | date: '%d%q of %b'}}
=>02nd of Feb
- Date literals are firstly converted to
Date
object via new Date(), that means literal values are considered in runtime’s time zone by default. - The format filter argument is optional:
- If not provided, it defaults to
%A, %B %-e, %Y at %-l:%M %P %z
. - The above default can be overridden by
dateFormat
LiquidJS option.
- If not provided, it defaults to
- LiquidJS
date
supports locale specific weekdays and month names, which will fallback to English whereIntl
is not supported.- Ordinals (
%q
) and Jekyll specific date filters are English-only. locale
can be set when creating Liquid instance. Defaults toIntl.DateTimeFormat().resolvedOptions.locale
).
- Ordinals (
Examples
{{ article.published_at | date: '%a, %b %d, %y' }} => Fri, Jul 17, 15
{{ "now" | date: "%Y-%m-%d %H:%M" }} => 2020-03-25 15:57
// equivalent to setting options.dateFormat = %d%q of %b %Y at %I:%M %P %Z
{{ '1990-12-31T23:30:28Z' | date: '%d%q of %b %Y at %I:%M %P %Z', -330 }} => 01st of Jan 1991 at 05:00 am +0530;
TimeZone
- During output, LiquidJS uses local timezone which can override by:
- setting a timezone in-place when calling
date
filter, or - setting the
timezoneOffset
LiquidJS option- It defaults to runtime’s time one.
- Offset can be set as,
- minutes:
-360
means'+06:00'
and360
means'-06:00'
- timeZone ID:
Asia/Colombo
orAmerica/New_York
- minutes:
- See here for TZ database values
- setting a timezone in-place when calling
Examples
// equivalent to setting `options.timezoneOffset` to `360`
{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", 360 }} => 1990-12-31T17:00:00
{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", "Asia/Colombo" }} => 1991-01-01T04:30:00
Input
date
works on strings if they contain well-formatted dates- Note that LiquidJS is using JavaScript Date to parse the input string, that means IETF-compliant RFC 2822 timestamps and strings in a version of ISO8601 are supported.
Examples
{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", 360 }} => 1990-12-31T17:00:00
{{ "March 14, 2016" | date: "%b %d, %y" }} => Mar 14, 16
Current Date
- To get the current time, pass the special word
"now"
or"today"
as input - Note that the value will be the current time of when the page was last generated from the template, not when the page is presented to a user if caching or static site generation is involved
Example
Last updated on: {{ "now" | date: "%Y-%m-%d %H:%M" }} => Last updated on: 2020-03-25 15:57
Last updated on: {{ "today" | date: "%Y-%m-%d %H:%M" }} => Last updated on: 2020-03-25 15:57