簡體   English   中英

JS 日期格式來自 mongoose 日期戳

[英]JS date format from mongoose date stamp

我知道類似的問題已被問過很多次,但沒有答案是我正在尋找的..

我的代碼如下所示:

        <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>

在 web 中查看它看起來像這樣:

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>

我實際上對 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> 

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM