|
1 | 1 | --- |
2 | 2 | title: date |
3 | 3 | --- |
4 | | - |
5 | 4 | {% since %}v1.9.1{% endsince %} |
6 | 5 |
|
7 | | -Converts a timestamp into another date format. LiquidJS tries to be conformant with Shopify/Liquid which is using Ruby's core [Time#strftime(string)](http://www.ruby-doc.org/core/Time.html#method-i-strftime). The input is firstly converted to `Date` object via [new Date()][newDate]. |
8 | | - |
9 | | -Input |
| 6 | +# Format |
| 7 | +* Converts a timestamp into another date format |
| 8 | +* LiquidJS tries to be conformant with Shopify/Liquid which is using Ruby's core [Time#strftime(string)](http://www.ruby-doc.org/core/Time.html#method-i-strftime) |
| 9 | + * Refer [format flags](https://ruby-doc.org/core/strftime_formatting_rdoc.html) |
| 10 | + * Not all options are supported though - refer [differences here](/tutorials/differences.html#Differences) |
| 11 | +* The input is firstly converted to `Date` object via [new Date()][jsDate] |
| 12 | +* Date format can be provided individually as a filter option |
| 13 | + * If not provided, then `%A, %B %-e, %Y at %-l:%M %P %z` format will be used as default format |
| 14 | + * Override this using [`dateFormat`](/api/interfaces/liquid_options_.liquidoptions.html#Optional-dateFormat) LiquidJS option, to set your preferred default format for all date filters |
| 15 | + |
| 16 | +### Examples |
10 | 17 | ```liquid |
11 | | -{{ article.published_at | date: "%a, %b %d, %y" }} |
12 | | -``` |
| 18 | +{{ article.published_at | date: '%a, %b %d, %y' }} => Fri, Jul 17, 15 |
| 19 | +{{ "now" | date: "%Y-%m-%d %H:%M" }} => 2020-03-25 15:57 |
13 | 20 |
|
14 | | -Output |
15 | | -```text |
16 | | -Fri, Jul 17, 15 |
| 21 | +// equivalent to setting options.dateFormat = %d%q of %b %Y at %I:%M %P %Z |
| 22 | +{{ '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; |
17 | 23 | ``` |
18 | 24 |
|
19 | | -{% note info TimeZone %} |
20 | | -Date will be converted to local time before output. To avoid that, you can set `timezoneOffset` LiquidJS option to `0`, its default value is your local timezone offset which can be obtained by `new Date().getTimezoneOffset()`. |
21 | | -{% endnote %} |
22 | | - |
23 | | -And you can set a timezone for each individual `date` filter via the second parameter: |
24 | | - |
25 | | -Input |
| 25 | +# TimeZone |
| 26 | +* By default, dates will be converted to local timezone before output |
| 27 | +* You can override that by, |
| 28 | + * setting a timezone for each individual `date` filter via the second parameter |
| 29 | + * using the [`timezoneOffset`](/api/interfaces/liquid_options_.liquidoptions.html#Optional-timezoneOffset) LiquidJS option |
| 30 | + * Its default value is your local timezone offset which can be obtained by `new Date().getTimezoneOffset()` |
| 31 | + * Offset can be set as, |
| 32 | + * minutes: `-360` means `'+06:00'` and `360` means `'-06:00'` |
| 33 | + * timeZone ID: `Asia/Colombo` or `America/New_York` |
| 34 | + * Use minutes for better performance with repeated processing of templates with many dates like, converting template for each email recipient |
| 35 | + * Refer [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) for TZ database values |
| 36 | + |
| 37 | +### Examples |
26 | 38 | ```liquid |
27 | | -{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", 360}} // equivalent to setting `options.timezoneOffset` to `360`. |
28 | | -{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", "Asia/Colombo" }} |
| 39 | +// equivalent to setting `options.timezoneOffset` to `360` |
| 40 | +{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", 360 }} => 1990-12-31T17:00:00 |
| 41 | +{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", "Asia/Colombo" }} => 1991-01-01T04:30:00 |
29 | 42 | ``` |
30 | 43 |
|
31 | | -Output |
32 | | -```liquid |
33 | | -1990-12-31T17:00:00 |
34 | | -1991-01-01T04:30:00 |
35 | | -``` |
36 | 44 |
|
37 | | -Input |
38 | | -```liquid |
39 | | -{{ article.published_at | date: "%Y" }} |
40 | | -``` |
| 45 | +# Input |
| 46 | +* `date` works on strings if they contain well-formatted dates |
| 47 | +* Note that LiquidJS is using [JavaScript Date][jsDate] to parse the input string, that means [IETF-compliant RFC 2822 timestamps](https://datatracker.ietf.org/doc/html/rfc2822#page-14) and strings in [a version of ISO8601](https://www.ecma-international.org/ecma-262/11.0/#sec-date.parse) are supported. |
41 | 48 |
|
42 | | -Output |
43 | | -```text |
44 | | -2015 |
45 | | -``` |
46 | | - |
47 | | -`date` works on strings if they contain well-formatted dates: |
48 | | - |
49 | | -Input |
| 49 | +### Examples |
50 | 50 | ```liquid |
51 | | -{{ "March 14, 2016" | date: "%b %d, %y" }} |
52 | | -``` |
53 | | - |
54 | | -Output |
55 | | -```text |
56 | | -Mar 14, 16 |
| 51 | +{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", 360 }} => 1990-12-31T17:00:00 |
| 52 | +{{ "March 14, 2016" | date: "%b %d, %y" }} => Mar 14, 16 |
57 | 53 | ``` |
58 | 54 |
|
59 | | -{% note info Timestamp Strings %} |
60 | | -Note that LiquidJS is using JavaScript [Date][newDate] to parse the input string, that means [IETF-compliant RFC 2822 timestamps](https://datatracker.ietf.org/doc/html/rfc2822#page-14) and strings in [a version of ISO8601](https://www.ecma-international.org/ecma-262/11.0/#sec-date.parse) are supported. |
61 | | -{% endnote %} |
62 | 55 |
|
63 | | -To get the current time, pass the special word `"now"` (or `"today"`) to `date`: |
| 56 | +# Current Date |
| 57 | +* To get the current time, pass the special word `"now"` or `"today"` as input |
| 58 | +* 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 |
64 | 59 |
|
65 | | -Input |
| 60 | +### Example |
66 | 61 | ```liquid |
67 | | -This page was last updated at {{ "now" | date: "%Y-%m-%d %H:%M" }}. |
68 | | -``` |
69 | | - |
70 | | -Output |
71 | | -```text |
72 | | -This page was last updated at 2020-03-25 15:57. |
| 62 | +Last updated on: {{ "now" | date: "%Y-%m-%d %H:%M" }} => Last updated on: 2020-03-25 15:57 |
| 63 | +Last updated on: {{ "today" | date: "%Y-%m-%d %H:%M" }} => Last updated on: 2020-03-25 15:57 |
73 | 64 | ``` |
74 | 65 |
|
75 | | -{% note info now %}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.{% endnote %} |
76 | | - |
77 | | -[newDate]: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date |
| 66 | +[jsDate]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date |
0 commit comments