Avatar
0
tvd12 Enlightened
tvd12 Enlightened
Thymeleaf: chuyển đổi timestamp hoặc date time về string tương ứng với time zone của client
Thymeleaf: chuyển đổi timestamp hoặc date time về string tương ứng với time zone của client
  • Answer
thymeleaf
Remain: 5
1 Answer
Avatar
monkey Enlightened
monkey Enlightened
Vì Thymeleaf chỉ render được html ở server nên chúng ta sẽ sử dụng javascript ở client để chuyển timestamp sang string:
Bước 1: Ở phía server bạn sẽ chuyển hết DateTime về dạng timestamp (long)
Bước 2: Code ở file template sẽ kiểu này:
<code>&lt;label <span><span>class</span></span>=<span>"date-time-string"</span> 
    th:text=<span>""</span>&gt;<span><span>&lt;/<span>label</span>&gt;</span></span>
</code>
Bước 3: Đặt đoạn code này ở cuối file trong thẻ script hoặc ở file js global
$( document ).ready(function() {
    $('.date-time-string').each((index, e) =&gt; {
        $(e).text(formatTimeStamp(parseInt($(e).text())));
    });
});
DONE.
  • 0
  • Reply