我似乎无法在我的 keystone JS 博客和其他页面上设置日期格式。
如果我在模板中使用 {{publishedDate}}(我正在使用 Handlebars ),一切都很好,除了它给我这样的东西:Thu Mar 23 2017 00:00:00 GMT+0000(GMT 标准时间)这显然不好。
{{ post.publishedDate.format('D MMMM') }}
{{ post._.publishedDate.format('D MMMM') }}
{{ publishedDate.format('D MMMM') }}
均返回错误如下: 第 18 行解析错误: ... post.publishedDate.format('D MMMM') }}< -----------------------^ 需要“ID”,却得到“INVALID”
我还尝试过更改模型中的内容:
publishedDate: { type: Types.Date, index: true, format: 'YYYY', dependsOn: { state: 'published' }},
及其众多变体。我查看了 keystone 文档:http://keystonejs.com/docs/database/#fieldtypes-date并尝试实现它所说的内容,但仍然不起作用。
这实际上应该非常简单,我确信它是 - 我只是不知道该怎么做(!)
感谢任何帮助。
最佳答案
下划线方法存在于文档中,而不是属性中。您的第二行代码应该可以工作; the format
underscore function将使用您所需的格式格式化 publishedDate
项(如果您不提供参数,则默认为 Do MMM YYYY
,但您可以通过提供来指定您自己的 Moment.js 格式format
的参数)。
{{ post._.publishedDate.format('D MMMM') }}
只要 post
作为本地变量提供给您的 Handlebars 模板(locals.post = post
在您的 route ),就不会返回任何错误。
编辑 2017 年 3 月 28 日
在 Handlebars 中,您必须以与 Keystone 文档描述不同的方式将数据传递给函数。他们使用 Pug,所以你想要做的事情在 Pug 中会起作用。 Handlebars 对功能的评估不同。尝试 {{post._.publishedDate.format "D MMMM"}}
将 "D MMMM"
字符串传递给 format,然后就可以正常工作了。
关于javascript - 设置 KeystoneJS 日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014978/