node.js - 如何在 pug 模板中运行 moment.js?

标签 node.js pug momentjs

我有一个控件,它将呈现带有对象数组的哈巴狗模板。

res.render('template.pug', {objects: objects}); 

在模板中,我将循环遍历数组并将日期修改为本地日期/时间。我想调用 moment 函数来实现这一点。

是否可以调用pug模板中的moment函数?或者有更好的解决方案吗?

最佳答案

您可以像这样在现有的 objects 数组中导入/传递 moment.js 库,

var objects= {
  someExitingKey1: 'Some value 1',
  someExitingKey2: 'Some value 2',
  ........,
  ........,
  ........,
  moment: require('moment')
};

在您的 template.pug 中,您可以使用 moment 库函数,例如,

doctype html
html
  head
    title= title
  body
    span= moment(someDateValue).format('YYYY-MM-DD')

希望这有帮助!

关于node.js - 如何在 pug 模板中运行 moment.js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52642494/

相关文章:

node.js - socket.io - xhr-polling 自动断开连接

Javascript、Jquery 和 jade : Dynamic forms not showing up in all the instances of relevant elements

python - 如何在 pyjade 中包含内联内容

asp.net-core - 使用 moment.js 将 UTC 时间戳发送到 ASP.NET Core Controller

javascript - 如何使用 Momentjs 获取昨天的日期?

momentjs - 如何使用 Aurelia/Typescript 导入 Moment-Timezone

javascript - 如何按顺序合并两个数组?

javascript - node.js - 使用 arrayFilter 在 mongoose findOneandUpdate 中嵌套数组

pug - 如何在 jade 中使用 angular2 html 绑定(bind)?特别是 a([router-link]= ["/Default"])?

node.js - 将标志传递给 Azure Web Apps 中的 NodeJS Chrome V8 引擎