简体   繁体   English

jslet通过小脚本在dd / MM / yyyy日期格式

[英]dd/MM/yyyy date format in jsp by scriptlet

I want to show the date coming from data base in the jsp page in dd/MM/yyyy format. 我想以dd / mm / yyyy格式在jsp页面中显示来自数据库的日期。 Here is my code in javascript. 这是我的JavaScript代码。

$(document).ready(function(){
    <%
         SDateDTO sDTO = (SDateDTO) request.getAttribute("sDTO");  
    if(null != scholAvailDTO){
        System.out.println(scholAvailDTO.getEndDate());
    %>

        var end = <%=scholAvailDTO.getEndDate() %>;
        $("#endDateId").val(end);
       <%       
    }
    %>

});

In console it is coming 27/04/2010 but in jsp it is getting populated like 0.0033582089552238806 which is actually division result of the date. 在控制台中,它即将于2010年4月27日到来,但在jsp中,它的填充像是0.0033582089552238806,实际上是日期的除法结果。 Any help will be appreciated. 任何帮助将不胜感激。 Thanks, Amit 谢谢阿米特

If getEndDate() returns java.util.Date object then, use DateFormat for get the formatted string and then wrap the value in quotes as below: 如果getEndDate()返回java.util.Date对象,则使用DateFormat获取格式化的字符串,然后将值包装在引号中,如下所示:

    <%DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");%>
    var end = "<%=formatter.format(scholAvailDTO.getEndDate()) %>";

If getEndDate() returns java.lang.String then simply wrap the value in quotes as below: 如果getEndDate()返回java.lang.String则只需将值包装在引号中,如下所示:

   var end = "<%=scholAvailDTO.getEndDate() %>";

Well yes - your Javascript will presumably be rendered to the browser as: 是的-您的Javascript可能会呈现为浏览器,如下所示:

var end = 27/04/2010;

If you want it to be in a string literal then you'll need to add the quotes yourself: 如果要使用字符串文字,则需要自己添加引号:

var end = "<%=scholAvailDTO.getEndDate() %>";

Note that you'll need to be confident that the value itself doesn't have quotes - or other values which aren't appropriate for JavaScript - in there. 注意,您需要确信该值本身没有引号-或其他不适合JavaScript的值。 I suspect there may well be a better approach than the above. 我怀疑可能有比上述更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM