1

I am trying to send back a date that the user put into the format dd/MM/yyyy. However, since my back end is in Java, when I create the object in the page (via Thymeleaf) where the date format is MM/dd/yyyy, it messes up the date.

<form th:action="@{/addDate}" th:object="${date}" method="POST" class="bd-example">
  <input type="text" class="form-control" id="dateInput" placeholder="DD/MM/YYYY" th:field="*{date}" required />

Is there any way to change this value in the page without modifying the method of the object in the back end?

honk
  • 9,137
  • 11
  • 75
  • 83
shas
  • 378
  • 4
  • 20
  • Parse the date to a different format in JS, have a look at https://stackoverflow.com/questions/10430321/how-to-parse-a-dd-mm-yyyy-or-dd-mm-yyyy-or-dd-mmm-yyyy-formatted-date-stri – Dennis Lukas Nov 13 '18 at 09:59
  • Have a hidden field which holds the value you want to use in the backend. Failing that, it shouldn't be hard to parse a different date format in Java. – Rory McCrossan Nov 13 '18 at 09:59
  • Use fake input for user and move value with proper format to actual hidden field using JS – Justinas Nov 13 '18 at 09:59
  • how is it possible to do that? – shas Nov 13 '18 at 10:00
  • @shas whenever the user types something (you can detect it with keyup, or if you prefer, you can wait until the finished typing and the control loses focus), run a JS function which copies the current content of the textbox to the hidden field, and transforms it along the way (for that you can use JavaScript date functions, or a library like momentJS, or just do some simple string manipulation). – ADyson Nov 13 '18 at 10:03
  • i use a jquerry datepicker, does it change anything? – shas Nov 13 '18 at 10:11
  • it shouldn't no. Why not try it and see? – ADyson Nov 13 '18 at 10:15
  • Did you managed to solve it? I had use datepicker before and I know getting it's value it quite tricky. – Alain Cruz Nov 13 '18 at 12:23
  • im still working on it, but https://stackoverflow.com/questions/7151543/convert-dd-mm-yyyy-string-to-date seems to work for datepicker, so im trying it. – shas Nov 13 '18 at 13:08

0 Answers0