设置 KeystoneJS 日期格式

Setting KeystoneJS Date format

我似乎无法在我的 keystone JS 博客和其他页面上设置日期格式。

如果我在模板中使用 {{ publishedDate }}(我使用的是车把),一切都很好,除了它给了我这样的东西:Thu Mar 23 2017 00:00:00 GMT+0000(GMT 标准时间)这显然不行。

{{ post.publishedDate.format('D MMMM') }}
{{ post._.publishedDate.format('D MMMM') }}
{{ publishedDate.format('D MMMM') }}

全部return报错如下: 第 18 行的解析错误: ... post.publishedDate.format('D MMMM') }}< ----------------------^ 期望 'ID',得到 'INVALID'

我也试过改变模型中的东西:

publishedDate: { type: Types.Date, index: true, format: 'YYYY', dependsOn: { state: 'published' }},

及其众多变体。我查看了位于 http://keystonejs.com/docs/database/#fieldtypes-date 的 keystone 文档,并尝试实现它在那里所说的内容,但它仍然无法正常工作。

这实际上应该非常简单,我敢肯定它是 - 我只是不知道该怎么做 (!)

感谢任何帮助。

文档中存在下划线方法,属性 中没有。您拥有的第二行代码应该可以工作; the format underscore function 将使用您想要的格式格式化 publishedDate 项(如果您不提供参数,则默认为 Do MMM YYYY,但您可以通过提供一个参数来指定自己的 Moment.js 格式format).

的参数
{{ post._.publishedDate.format('D MMMM') }}

只要将 post 作为局部变量提供给您的 Handlebars 模板(在您的路线中 locals.post = post),就不会 return 任何错误。

编辑 2017 年 3 月 28 日

在 Handlebars 中,您必须以不同于 Keystone 文档描述的方式将数据传递给函数。他们使用 Pug,因此您尝试做的事情会在 Pug 中起作用。 Handlebars 以不同的方式评估功能。尝试 {{post._.publishedDate.format "D MMMM"}}"D MMMM" 字符串传递给格式,然后它就可以正常工作了。

谢谢 Shea - 超出职责范围的帮助。非常感谢 ;-) 最终解决方案(对于任何其他感兴趣的人)是 - 如果 运行 在一个循环中 - 在模型中创建一个虚拟,如下所示:

Post.schema.virtual('formattedDate').get(function () { 
    return this._.publishedDate.format("D MMMM"); 
});

然后,在把手模板中使用以下格式:

{{formattedDate}}

If not 运行 in a loop can be done simply with:

{{_.publishedDate.format "D MMMM"}}