我在转换方面遇到了一些麻烦。它在alert(first_date) and alert(end_date)
等时显示无效日期:s我使用datetimepicker来获取日期。
$( "#start_date" ).datetimepicker({
dateFormat : 'yy-mm-dd',
timeFormat: 'hh:mm:ss',
defaultDateTime: "+1w",
showSecond: true,
changeMonth: true,
changeYear: true,
onClose: function( selectedDateTime ) {
$( "#end_date" ).datetimepicker( "option", selectedDateTime );
}
});
$( "#end_date" ).datetimepicker({
dateFormat : 'yy-mm-dd',
timeFormat: 'hh:mm:ss',
defaultDateTime: "+1w",
showSecond: true,
changeMonth: true,
changeYear: true,
onClose: function( selectedDateTime ) {
$( "#start_date" ).datetimepicker( "option", selectedDateTime );
}
});
现在,我需要做这样的事情:
var first_date = new Date($('#start_date').val());
var last_date = new Date($('#end_date').val());
var nDifference = (last_date - first_date);
var one_day = 1000*60*60*24;
var days = Math.round(nDifference/one_day);
<input type="text" id="start_date" name="start_date" value=""/>
<input type="text" id="end_date" name="end_date" value=""/>
答案 0 :(得分:3)
Javascript日期可靠地接受ISO日期格式......
d = '2012-01-01 06:16:16'
console.log(new Date(d)) // invalid
console.log(new Date(d.replace(' ','T'))) // valid!
// Z at the end accounts for local timezone offset
console.log(new Date(d.replace(' ','T')+'Z'))