Формат даты JS из штампа Мангуста - PullRequest
0 голосов
/ 13 октября 2019

Я знаю, что подобные вопросы задавались много раз, но я не нашел ответа на вопрос ...

Мой код выглядит так:

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

При просмотре в Интернете это выглядит так:

Пт 27 сентября 2019 17:38:08 GMT + 0200 (стандартное время Южной Африки)

Как я хочу, чтобы это выглядело:

27 сентября 2019

Возможно ли использование moment.js?

Что япробовал: вверху:

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

, а затем (жестко закодированная дата для целей тестирования)

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

1 Ответ

0 голосов
/ 13 октября 2019

Я на самом деле очень плохо знаком с 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> 

Надеюсь, это поможет

...