【问题标题】:Change date format for input date and pass to the function only actual date更改输入日期的日期格式并仅传递给函数实际日期
【发布时间】:2021-05-05 17:16:26
【问题描述】:

我想把我的日期当作输入日期的值

原始格式:OffsetDateTime 格式

例如:我有这个2021-08-21T14:06:58.147+03:00,我想要2021-08-21

 <tr class="active-row" th:each="client : ${trialUsers}">
          <td th:text="${client.firstName}"></td>
          <td th:text="${client.lastName}"></td>
          <td th:text="${client.email}"></td>
  <td> <input type="date" id="dateForUpdate" th:value="${client.myTime}"> </td>
        <td> <button class="btn" th:attr="onclick='testFunc(\'' + ${client.myTime} +'\',\''+ ${client.email} + '\');'">Extend period</button> </td>
          </td>
        </tr> 

我有这张桌子

 name | lastname | email           | time |  Change time

[Mark] [Jones]     [mark@gmail.com] [2021-08-21T14:06:58.147+03:00]

 [Button for extend period]

但我想要这个:

[Mark] [Jones] [mark@gmail.com] [2021-08-21] [Button for extend period]

我尝试使用这个${#dates.format(client.myTime, 'yyyy-MM-dd')} 但它不起作用。

我还有一个问题。如何始终将实际日期值传递给 myFunc()?毕竟,用户可以更改日期并只发送旧日期。

【问题讨论】:

  • 欢迎来到 Stack Overflow!访问help center,使用tour 了解内容和How to Ask。请首先>>>Search for related topics on SO,如果您遇到困难,请发布您的尝试的minimal reproducible example,并使用[&lt;&gt;] 记录输入和预期输出sn-p 编辑器。
  • 请发布相关脚本和 HTML,而不是一些 ascii 渲染。您可能只需要在“T”上拆分
  • #dates.format() 函数用于java.util.Date 对象。你有一个java.time.OffsetDateTime 对象。因此,您需要改用 Thymeleaf #temporals.format() 函数。这是在 Thymeleaf 附加 JAR 文件中提供的。
  • @CringePointerException 如果您找到了解决方案,请发布并接受您自己的问题的答案,以便将此页面标记为已解决,以便未来的读者受益。

标签: java html datetime thymeleaf


【解决方案1】:
    First answer: The #dates.format() function is for java.util.Date objects. You have a java.time.OffsetDateTime object. Therefore you need to use a Thymeleaf #temporals.format() function, instead. This is provided in a Thymeleaf add-on JAR file.

谢谢@andrewjames

这是第二个问题的解决方案。如果你只想通过实际 输入日期使用这个:

<table>
        <thead>
        <tr>
          <td>name</td>
          <td>surname</td>
          <td>Email</td>
          <td>time</td>
          <th>action</th>
        </tr>
        </thead>
        <tbody>
        <tr class="active-row" th:each="client : ${trialUsers}">
          <td th:text="${client.firstName}"></td>
          <td th:text="${client.lastName}"></td>
          <td th:text="${client.email}"></td>
          <td> <input type="date" th:id='${client.id}' th:value="${#temporals.format(client.time, 'yyyy-MM-dd')}"> </td>
          <td> <button class="btn" th:attr="onclick='testFunc(\''+ ${client.id} + '\');'">change time</button> </td>
          </td>
        </tr>
        </tbody>
        <script>
          function testFunc(timeId) {
          var myTime =  document.getElementById(timeId).value;

            fetch('/register/trial/changeTime', {
              method: 'put',
              headers: {'Content-Type':'application/json'},
              body: JSON.stringify({
                "id": timeId,
                "time": myTime,
              })
            })

                .then(function (response) {
                  return response.json();
                })
                .then(function (result) {
                 console.log("blabla")
                .
                catch (function (error) {
                  console.log("blabla")
                });
          }
        </script>
      </table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    相关资源
    最近更新 更多