【问题标题】:jquery tmpl formatting a date?jquery tmpl 格式化日期?
【发布时间】:2011-09-20 13:57:23
【问题描述】:

我正在使用 jquery tmpl 在表格中显示一堆结果。其中之一是我在模板中使用它输出的日期:

<td class="textAlignRight">${EffectiveDate}</td>

但它的格式类似于“/Date(1245398693390)/”。如何更改它以使其格式像 m/dd/yyyy h:mm tt 一样?

【问题讨论】:

    标签: jquery json templates


    【解决方案1】:

    只需使用一个函数来格式化您的日期:

    模板:

    <td class="textAlignRight">${GetDate(EffectiveDate)}</td>
    

    功能:

    function GetDate(jsonDate) {
      var value = new Date(parseInt(jsonDate.substr(6)));
      return value.getMonth() + 1 + "/" + value.getDate() + "/" + value.getFullYear();
    }
    

    【讨论】:

    • 究竟需要在哪里粘贴GetDate()函数?在ready?
    【解决方案2】:
    <td class="textAlignRight">{{= format(new Date(parseInt(EffectiveDate.substr(6))), 'd') }}</td>
    

    【讨论】:

      【解决方案3】:

      我建议使用这样的东西:

      <script type='text/javascript'>
          Date.prototype.CustomFormat = function () {
              return this.getMonth() + 1 + "/" + this.getDate() + "/" + this.getFullYear();
          };
      </script>
      

      ...

      <td class="textAlignRight">${EffectiveDate.CustomFormat()}</td>
      

      【讨论】:

        猜你喜欢
        • 2013-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-06
        • 2011-01-30
        • 2015-09-21
        相关资源
        最近更新 更多