首页 > 解决方案 > 将自定义日期格式添加到 JSP html 代码

问题描述

<td>${item.actionDate}</td>接收格式为 2019-11-17 (yyyy/mm/dd) 的日期。我想将此格式编辑为 dd/mm/yyyy。我发现我可以使用下面的方法为这个 td 元素提供格式。

<script type="text/javascript">
  $(document).ready(function() {
    $('#serial_no_date_format').datepicker({
      format: 'dd/mm/yyyy'
    });
   })
</script>

我知道上面的代码片段是正确的,因为它在我的代码的其他部分中工作。但是,我无法将此日期格式传递给带有 id 参考的 td.item.actionDate 元素。

我的整体在下面

<div class="tab-pane fade active in" id="tab_1_1">
            <div class="row">
                <div class="col-md-12">
                    <table class="table table-striped table-bordered table-hover order-column">
                        <thead>
                        <tr>
                            <th>Sevkiyat No</th>
                            <th>Operator</th>
                            <th>İşlem Tarihi</th>
                            <th>İşlem</th>
                            <th>Lokasyon</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${shipmentTransferHistoryList}" var="item">
                            <tr>
                                <td>${item.shipmentDetailId}</td>
                                <td>${item.operator}</td>
                                <td>${item.actionDate} id="serial_no_date_format"</td>
                                <td>${item.shipmentStatus.alias}</td>
                                <td>${item.actionLocation}</td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>

                    <script type="text/javascript">
                        $(document).ready(function() {
                            $('#serial_no_date_format').datepicker({
                                format: 'dd/mm/yyyy'
                            });
                        })
                    </script>
                </div>
            </div>
        </div>

我怎样才能做到这一点?

标签: javascripthtmljsp

解决方案


您的 id 应设置在标签内。

<td>${item.actionDate} id="serial_no_date_format"</td>

改成这个

<td id="serial_no_date_format">${item.actionDate} </td>

请注意,您可能会遇到重复 id 的问题,因此如果可能,请将其更改为 class。

编辑

我想这会解决你的问题。

var date = $('#serial_no_date_format').datepicker({format: 'dd/mm/yyyy'}).val();
$('#serial_no_date_format').val(date);

推荐阅读