我正在尝试设置默认日期,当我使用12/29/2016日期格式时它会起作用,但在使用12-19-2016格式时无效。我如何将其转换为2016年12月29日?
我添加了这段代码:
var businessDate = $("#businessDate").val();
var date = new Date(businessDate);
现在,我得到12/28/2016而不是2016年12月29日。我该如何解决这个问题?
$(function() {
var dateFormat = "mm/dd/yy", from = $("#startDate").datepicker({
defaultDate : "+1w",
changeMonth : true,
changeYear : true,
numberOfMonths : 1,
beforeShowDay : $.datepicker.noWeekends
}).on("change", function() {
from.datepicker("option", "minDate", getDate(this));
}), to = $("#endDate").datepicker({
defaultDate : "+1w",
changeMonth : true,
changeYear : true,
numberOfMonths : 1,
beforeShowDay : $.datepicker.noWeekends
}).on("change", function() {
to.datepicker("option", "maxDate", getDate(this));
});
var businessDate = $("#businessDate").val(); //12-29-2019
$("#startDate").datepicker("setDate", "12/29/2017"); // Works
$("#startDate").datepicker("setDate", businessDate ); // Does not work
HTML
<div class=rows>
<span class="label"><strong>Date Range:</strong></span>
<div id="dateToAndFrom">
<span class="dateFrom"> <label id="dateLabel" for="from">From</label>
<input type="text" id="startDate" name="startDate">
</span>
<span class="dateTo"> <label id="dateLabel" for="to">To</label>
<input type="text" id="endDate" name="endDate">
</span>
</div>
</div>
</div>
<input type="hidden" id="businessDate" name="businessDate"
value="${businessDate}" />
答案 0 :(得分:1)
这样做:
var businessDate = $("#businessDate").val(); //12-29-2019
businessDate = new Date(businessDate)
$("#startDate").datepicker("setDate", businessDate );