javascript 在 Underscore 模板中格式化日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18628479/
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-10-27 12:39:53  来源:igfitidea点击:

Formatting dates in Underscore templates

phpjavascriptdatebackbone.jsunderscore.js

提问by Sanny Singhs

I have a dateattribute in a Backbone model used in an underscore template.
The datevalue is in integer format like 123456432.

date下划线模板中使用的 Backbone 模型中有一个属性。
date值采用整数格式,如123456432

I want to show this integer value in dd / mm / yyyyformat in an underscore template, like the way I do in PHP.

我想在下划线模板中以dd / mm / yyyy格式显示这个整数值,就像我在 PHP 中所做的那样。

Here is my underscore template

这是我的下划线模板

<script type="text/template" id="item-template">    
    <span class="label label-info"><%- name %> <em> <%= date %> </em>  </span>
</script>

回答by nikoshr

Underscore templates let you call function and output text in any way you see fit via print. For example, to convert your timestamp to a date, you could use something like this

下划线模板让您可以通过print. 例如,要将时间戳转换为日期,您可以使用这样的方法

<script type="text/template" id="tpl-1">    
    <span class="label label-info"><% print(new Date(date*1000)) %></span>
</script>

Note that I assume the timestamp comes from PHP, thus in seconds. In Javascript, the timestamps are expected to be in milliseconds, that's why I multiply it by 1000.

请注意,我假设时间戳来自 PHP,因此以秒为单位。在 Javascript 中,时间戳预计以毫秒为单位,这就是我将其乘以 1000 的原因。

If your timestamps come from Javascript, use

如果您的时间戳来自 Javascript,请使用

<script type="text/template" id="tpl-1">    
    <span class="label label-info"><% print(new Date(date)) %></span>
</script>

Formatting this date objectcould be done like this

格式化这个日期对象可以这样完成

<script type="text/template" id="tpl-2">    
    <span class="label label-info"><% 
        var d = new Date(date*1000), // or d = new Date(date)
            fragments = [
                d.getDate(),
                d.getMonth() + 1,
                d.getFullYear()
            ]; 
            print(fragments.join('/'));
        %></span>
</script>

Or factorize all this into a function call (here on _.templatebut you could store it anywhere)

或者将所有这些分解为一个函数调用(在这里,_.template但你可以将它存储在任何地方)

_.template.formatdate = function (stamp) {
    var d = new Date(stamp*1000), // or d = new Date(date)
        fragments = [
            d.getDate(),
            d.getMonth() + 1,
            d.getFullYear()
        ]; 
    return fragments.join('/');
};
<script type="text/template" id="tpl-3">    
    <span class="label label-info"><%= _.template.formatdate(date) %></span>
</script>

And a demo http://jsfiddle.net/Dyzm8/

和一个演示http://jsfiddle.net/Dyzm8/

回答by Blacksonic

You have 2 options:

您有 2 个选择:

When passing the result of the toJSON method, extend the object with another field:

在传递 toJSON 方法的结果时,使用另一个字段扩展对象:

var fields = this.model.toJSON();
fields.formattedDate = '29/11/1973';
this.template(fields);

The other way is to have a look at Backbone Marionette, which has helper methodsfor such cases (and solves many other repetitive tasks).

另一种方法是查看Backbone Marionette,它具有针对此类情况的辅助方法(并解决了许多其他重复性任务)。

A simple PHP date function port in Javascript can be found here.

可以在此处找到 Javascript 中的简单 PHP 日期函数端口。