【问题标题】:JS date format from mongoose date stamp来自猫鼬日期戳的JS日期格式
【发布时间】:2020-02-10 08:56:40
【问题描述】:

我知道类似的问题已经被问过很多次,但没有答案是我想要的。

我的代码如下所示:

        <table id="dtBasicExample" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
            <thead>
                <tr>
                <th class="th-sm">Date Requested
                </th>
                <th class="th-sm">Client
                </th>
                <th class="th-sm">Waybill
                </th>
                <th class="th-sm">From
                </th>
                <th class="th-sm">To
                </th>
                <th class="th-sm">Service
                </th>
                <th class="th-sm">Supplier
                </th>
                <th class="th-sm">kg
                </th>
                <th class="th-sm">Supplier
                </th>
                <th class="th-sm">VAT
                </th>
                <th class="th-sm">Total
                </th>
                <th class="th-sm">Client
                </th>
                <th class="th-sm">VAT
                </th>
                <th class="th-sm">Total
                </th>
                <th class="th-sm">Profit
                </th>
                <th class="th-sm">Status
                </th>
                <th class="th-sm">Modify
                </th>
                </tr>
            </thead>
            <tbody>
                <% for (let x of collections) {%>
                <tr style="<%= x.collectionStatusId.status == 'Requested' ? 'background-color: pink; color: black' : '' %>">
                    <td><%-x.createdAt%></td>
                    <td><%-x.client.companyName%></td>
                    <td><%-x.waybill%></td>
                    <td><%-x.sender.company%></td>
                    <td><%-x.receiver.company%></td>
                    <td><%-x.serviceId? x.serviceId.serviceCode : ''%></td>
                    <td><%-x.supplier? x.supplier.name : ''%></td>
                    <td><%-x.chargeableWeight%></td>
                    <td><%-x.supplierRate%></td>
                    <td><%-x.supplierVat%></td>
                    <td><%-x.supplierRate + x.supplierVat%></td>
                    <td><%-x.myRate%></td>
                    <td><%-x.myVat%></td>
                    <td><%-x.myRate + x.myVat%></td>
                    <td><%-(x.myRate - x.supplierRate)%></td>
                    <td><%-x.collectionStatusId.status%></td>
                    <td><a href="/admin/<%- x.collectionStatusId.status == 'Requested' ? 'accept' : 'edit' %>/<%- x.waybill %>"><%- x.collectionStatusId.status == 'Requested' ? 'Accept' : 'Update' %></a></td>
                </tr>
                <% } %>

            </tbody>
            <tfoot>
                <tr>
                <th class="th-sm">Date Requested
                </th>
                <th class="th-sm">Client
                </th>
                <th class="th-sm">Waybill
                </th>
                <th class="th-sm">From
                </th>
                <th class="th-sm">To
                </th>
                <th class="th-sm">Service
                </th>
                <th class="th-sm">Supplier
                </th>
                <th class="th-sm">kg
                </th>
                <th class="th-sm">Supplier
                </th>
                <th class="th-sm">VAT
                </th>
                <th class="th-sm">Total
                </th>
                <th class="th-sm">Client
                </th>
                <th class="th-sm">VAT
                </th>
                <th class="th-sm">Total
                </th>
                <th class="th-sm">Profit
                </th>
                <th class="th-sm">Status
                </th>
                <th class="th-sm">Modify
                </th>
                </tr>
            </tfoot>
        </table>

专注于这一行:

<td><%-x.createdAt%></td>

在网上看是这样的:

2019 年 9 月 27 日星期五 17:38:08 GMT+0200(南非标准时间)

我想要的样子:

2019 年 9 月 27 日

使用 moment.js 可以吗?

我尝试过的: 在顶部:

<script src="/js/moment.js"></script>

然后(用于测试目的的硬编码日期)

<td><script>moment('1977-08-20 14:29:00 UTC').format('dd MMM YYYY')</script>   </td>

【问题讨论】:

    标签: javascript node.js mongoose momentjs


    【解决方案1】:

    我其实对 JS 很陌生,我花了几分钟才弄明白,但实际上真的很简单..

    <td><%-x.createdAt%></td>
    

    改为

    <td class="formattedDate"><%-x.createdAt%></td>
    

    在添加以下内容之前的底部:

    <script src="/js/jquery-3.3.1.js"></script>
    
    <script>
    $(document).ready(function () {
        var x = document.getElementsByClassName('formattedDate');
        for (i = 0; i < x.length; i++) {
            x[i].innerHTML = moment(x[i].innerHTML).format('DD MMM YYYY');
        }
    });
    </script> 
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2013-05-15
      • 1970-01-01
      • 2020-06-11
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 2016-10-01
      • 1970-01-01
      相关资源
      最近更新 更多