Java date format to JavaScript date format

后端 未结 10 2081
余生分开走
余生分开走 2020-12-03 01:10

I would like to be able to convert a Java date format string, e.g. dd/MM/yyyy (07/06/2009) to a JavaScript date format string, e.g. dd/mm/yy (07/06/2009).

相关标签:
10条回答
  • 2020-12-03 01:33

    If you just need to pass a date from Java to JavaScript, the best way to do it, I think, would be to convert the Java date to milliseconds using date.getTime(), create a JavaScript date initialized with this milliseconds value with new Date(milliseconds)and then format the date with the means of the JavaScript Date object, like: date.toLocaleString().

    0 讨论(0)
  • 2020-12-03 01:34

    This works fine for me:

    <%
    Date date = Calendar.getInstance().getTime();
    %>
    
    <script>
    var d = new Date(<%=date.getTime()%>);
    alert(d);
    </script>
    
    0 讨论(0)
  • 2020-12-03 01:35

    I suggest you the MomentJS with this Plugin that allow you to convert a Java pattern to a JS pattern (MomentJS)

    0 讨论(0)
  • 2020-12-03 01:36

    If you just want to format dates my date extensions will do that well - it also parses data formats and does a lot of date math/compares as well:

    DP_DateExtensions Library

    Not sure if it'll help, but I've found it invaluable in several projects.

    0 讨论(0)
提交回复
热议问题