如何在Pug元素中调用JavaScript函数

时间:2018-07-18 11:45:52

标签: javascript pug

each customer, i in customers        
    tr
        td #{timeConverter(customer.joiningDate)} 


    function timeConverter(UNIX_timestamp){
        var a = new Date(UNIX_timestamp * 1000);
        var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
        var year = a.getFullYear();
        var month = months[a.getMonth()];
        var date = a.getDate();
        var hour = a.getHours();
        var min = a.getMinutes();
        var sec = a.getSeconds();
        var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
        return time;
      }

3 个答案:

答案 0 :(得分:0)

尝试一下:

each customer, i in customers        
    tr
        script.
          var x;
          x=timeConverter(customer.joiningDate);
        td #{x} 

答案 1 :(得分:0)

我找到了答案。我为该函数创建了一个路由,初始化了一个全局变量,并在前端使用了它。

答案 2 :(得分:-1)

enter image description here您可以检查一下吗?

将此添加为第一行

- var timeConverter = function (UNIX_timestamp){ var a = new Date(UNIX_timestamp * 1000);var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];var year = a.getFullYear();var month = months[a.getMonth()];var date = a.getDate();var hour = a.getHours();var min = a.getMinutes();var sec = a.getSeconds();var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;return time;}

在此行下方添加您的PUG代码

  

doctype html   html     头       标题         我的编PUG     身体       [{joiningDate:new Date()}]中的每个客户,
       TR         td#{timeConverter(customer.joiningDate)}

然后运行:)

尝试https://pughtml.com/