JQuery datepicker预先填充错误的格式

时间:2014-03-14 11:41:36

标签: jquery datepicker

我在JSP文件中有这个JavaScript:

         <script type="text/javascript">
        $(document).ready(function() {
            $('#startDate,#endDate').datepicker({
                beforeShow: customRange,
                //defaultDate: "-1w",
                dateFormat: "dd/mm/yy",
                firstDay: 0,               // first day of the week is Sunday
                numberOfMonths : 2,        // show two months at a time
                changeMonth: true,         // allow changing the month
                changeYear: true           // allow changing the year
        }).attr('readonly', 'readonly');

    });

    function customRange(input) { 
            var min = new Date()
            ,dateMin = null
            ,dateMax = min
           // ,dayRange = 90 // Set this to the range of days you want to restrict to
            ;

            if (input.id === "endDate") {
                    if ($("#startDate").datepicker("getDate") != null) {
                            dateMin = $("#startDate").datepicker("getDate");
                    }
            }

            return {
                    minDate: dateMin, 
                    maxDate: dateMax
            };     

    }    
</script>

当页面加载startDate和endDate文本框时:&#34; Fri Mar 14 13:54:08 WST 2014&#34;但应该在&#34; dd / mm / yy&#34;格式。

另外,我还希望startDate的初始值为7天前。

关于这些的任何想法?

由于

2 个答案:

答案 0 :(得分:1)

您获得的日期格式是默认的java.util.Date格式,例如:

System.out.println(new Date());
// prints: Mon Mar 17 09:41:25 WST 2014

而在浏览器控制台中等效的JavaScript:

new Date();
// prints: Mon Mar 17 2014 09:42:04 GMT+0800 (W. Australia Standard Time)

因此,您需要通过formatDate标记格式化Java日期:

<fmt:formatDate var="formattedStartDate" pattern="dd/MM/yyyy" value="myForm.startDate" />
<input id="startDate" type="text" value="${formattedStartDate}" />

答案 1 :(得分:0)

使用以下代码

将初始值分配给startDate
$( ".selector" ).datepicker({ maxDate: "-7d" });


// getter
var maxDate = $( ".selector" ).datepicker( "option", "maxDate" );
// setter
$( ".selector" ).datepicker( "option", "maxDate", "-7d" );

使用以下代码设置日期格式

$( ".selector" ).datepicker({ dateFormat: "dd/mm/yy" });
// getter
var dateFormat = $( ".selector" ).datepicker( "option", "dateFormat" );
// setter
$( ".selector" ).datepicker( "option", "dateFormat", "dd/mm/yy" );

要了解有关datepicker选项的更多信息,请查看此link