我如何格式化[未来,非流动]日期。 nunjucks?
我正在为本地乐队建立一个带有CMS(Netlify)的网站,他们将在网站上放置未来的演出日期。到目前为止,日期显示为包括时间和时区的非常长的非格式字符串。我正在尝试弄清楚如何将日期格式化更简单(例如,日期,时间)。
我尝试了像Nunjucks-Date这样的插件,但是在这种情况下,我对如何使用插件(和过滤器)有些困惑。
我的仓库: https://github.com/mollycarloll/serapis-serapis-serapis-earpis-exty-20-2
示例演出条目:
---
layout: gig
venue: Cedar Lake Cellars
date: 2022-05-28
time: 6pm
city: Wright City, MO
---
演出模板:
<h2>{{ venue }}</h2>
<h4>{{ city }} {{ date }} {{ time }}</h4>
config.yml for cms:
- name: 'gigs'
label: 'Shows'
folder: 'src/gigs'
create: true
slug: '{{month}}-{{day}}-{{venue}}'
fields:
- { label: 'Layout', name: 'layout', widget: 'hidden', default: '_includes/gig.njk' }
- { label: 'Date', name: 'date', widget: 'date', default: '' }
- { label: 'Time', name: 'time', widget: 'string', default: '' }
- { label: 'Venue', name: 'venue', widget: 'string', default: '' }
- { label: 'City', name: 'city', widget: 'string', default: '' }
感谢您的任何帮助。
I'm building a site with a CMS (Netlify) for a local band, and they have future gig dates they will put on the site. So far the dates show up as very long non-formatted strings that include the time and time zone. I'm trying to figure out how to format the dates to be simpler (day, date, time for example).
I've tried plugins like nunjucks-date but I'm a little confused about how to use a plugin (and filters) in this case.
My repo: https://github.com/mollycarroll/serapis-eleventy-2
Example gig entry:
---
layout: gig
venue: Cedar Lake Cellars
date: 2022-05-28
time: 6pm
city: Wright City, MO
---
Gig template:
<h2>{{ venue }}</h2>
<h4>{{ city }} {{ date }} {{ time }}</h4>
config.yml for the CMS:
- name: 'gigs'
label: 'Shows'
folder: 'src/gigs'
create: true
slug: '{{month}}-{{day}}-{{venue}}'
fields:
- { label: 'Layout', name: 'layout', widget: 'hidden', default: '_includes/gig.njk' }
- { label: 'Date', name: 'date', widget: 'date', default: '' }
- { label: 'Time', name: 'time', widget: 'string', default: '' }
- { label: 'Venue', name: 'venue', widget: 'string', default: '' }
- { label: 'City', name: 'city', widget: 'string', default: '' }
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然user34888304很好,但它无法处理传入值是date()对象的情况,在下面类似的情况下
,您可能希望更将其编写为
./ src/filters /date.js
当然不要忘记在.eleventy.js中添加过滤器:
While user3488304's is good, it doesn't handle cases where the incoming value is a Date() object which is what you would see in cases like below
So you may want to write it more like this for
./src/filters/date.js
And of course don't forget to add the filter in .eleventy.js:
首先,您应该创建一个过滤器,假设
src/filters/date.js
具有以下内容:确保您查看Luxon文档以获取详细信息。然后在.eleventy.js中添加过滤器:
现在您可以在带有默认值
{{date}}}
的nunjack中使用它,在此示例'llll d,y'或您需要的任何值您的网站上的某个位置{{date | date('dd。llllyyyy。')}}}
。如果您需要在一个月和每年或日常和每月的某个时候需要,这将非常有用。您甚至可以创建多种语言过滤器,例如
dateen.js
和dede.js
,如果您有多语言网站,则将各种格式化为自己的语言。希望这会有所帮助。
编辑:为了使此过滤器工作
dateObj
应采用ISO 8601格式。First, you should create a filter, let's say
src/filters/date.js
with the following content:Make sure you check Luxon documentation for details. Then add the filter in .eleventy.js:
Now you can use it in Nunjacks with a default value
{{ date }}
, in this example 'LLLL d, y', or any value you need at a certain position on your website{{ date | date('dd. LLLL yyyy.') }}
. This can be very useful if you need at some point just month and year or just day and month.You can even create multiple language filters, like
dateEn.js
anddateDe.js
, and format each to its own language if you have a multilingual site.Hope this helps.
EDIT: In order for this filter to work the
dateObj
should be in ISO 8601 format.