0

我在转换时遇到了一些麻烦。它说日期无效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=""/>
4

1 回答 1

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'))
于 2012-11-28T11:41:38.273 回答