node.js 试图在玉模板上格式化日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16922625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:43:31  来源:igfitidea点击:

trying to format a date on jade template

node.jspug

提问by babbaggeii

I have an index.js:

我有一个 index.js:

exports.index = function(req, res){
  db.courses.find(function(err, currentCourses) {
    res.render('index', {
      currentCourses: currentCourses
    });
  });
};

And on my jade template:

在我的玉模板上:

tr
    td #{currentCourses[0].start}

Which is a date, formatted as "Sun Sep 29 2013 00:00:00 GMT+0100 (BST)".

这是一个日期,格式为“Sun Sep 29 2013 00:00:00 GMT+0100 (BST)”。

How can I format it to "29 Sep 2013"?

如何将其格式化为“2013 年 9 月 29 日”?

Edit (after Ed Hinchliffe's comments):

编辑(在 Ed Hinchliffe 的评论之后):

-function prettyDate(dateString){
    -var d = date.getDate(dateString);
    -var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
    -var m = monthNames[date.getMonth()];
    -var y = date.getFullYear();
    -return d+' '+m+' '+y;
-} 
for course in currentCourses
    tr
        td #{prettyDate(course.start)}

采纳答案by Ed Hinchliffe

Not particularly easy unfortunately. You'll need a function to format a string either inside your template, or outside (and pass the pretty string).

不幸的是不是特别容易。您需要一个函数来格式化模板内部或外部的字符串(并传递漂亮的字符串)。

Something like this (JADE)

像这样的东西(玉)

-function prettyDate(dateString){
    //if it's already a date object and not a string you don't need this line:
    -var date = new Date(dateString);
    -var d = date.getDate();
    -var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
    -var m = monthNames[date.getMonth()];
    -var y = date.getFullYear();
    -return d+' '+m+' '+y;
-}


tr
   td #{prettyDate(currentCourses[0].start)}

回答by Zhifeng Hu

My solution is:

我的解决办法是:

Add momentjsto your express application locals like this:
app.locals.moment = require('moment');

像这样将momentjs添加到您的 express 应用程序本地:
app.locals.moment = require('moment');

Then you can use moment in any jade files:
span='(Created at: ' + moment(obj.createTime).format("YYYY/MM/DD") + ')'

然后你可以在任何玉文件中使用 moment:
span='(Created at: ' + moment(obj.createTime).format("YYYY/MM/DD") + ')'

Reference:
Making use of utility libraries in server-side Jade templates

参考:
在服务器端 Jade 模板中使用实用程序库

回答by morecore

The above solution from Zhifeng Hugave me the right direction. Unfortunately app.locals.moment did not worked for me.

胡志峰的上述解决方案给了我正确的方向。不幸的是 app.locals.moment 对我不起作用。

But you can pass the require('moment')also directly into the object for the template attributes.

但是您也可以将require('moment')也直接传递到模板属性的对象中。

var data = {
  title: 'some nice title',
  updateDate: new Date(),
  ....,
  moment: require( 'moment' )
};

And then pass the data object as usual to the template function.

然后像往常一样将数据对象传递给模板函数。

var template = pug.compile( source );
var html = template( data );

Source File example:

源文件示例:

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

回答by McB

I like to take an approach similar to @Zhifeng Hu, but instead of requiring everything into locals, I just require "require", then I can pull things in as needed in my templates.

我喜欢采用类似于@Zhifeng Hu 的方法,但不是将所有内容都要求本地化,我只需要“require”,然后我就可以根据需要在我的模板中拉入东西。

app.use((req, res, next) => { res.locals.require = require; next() })

app.use((req, res, next) => { res.locals.require = require; next() })

and then in Jade/Pug

然后在玉/帕格

- const moment = require('moment')
div Created at: #{moment(data.createdAt).fromNow()}

Basically the same thing, but I can keep the require code in the template where it's used.

基本上是一样的,但我可以将 require 代码保留在使用它的模板中。

回答by sumek

You should format the date server-side. Limit the amount of logic done inside the template to bare minimum - ideally nothing at all.

您应该格式化日期服务器端。将模板内完成的逻辑量限制在最低限度 - 最好什么都不做。