Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript new date()转换_Javascript_Date_Datetimepicker - Fatal编程技术网

Javascript new date()转换

Javascript new date()转换,javascript,date,datetimepicker,Javascript,Date,Datetimepicker,我在转换方面遇到了一些麻烦。当警报(第一个日期)和警报(结束日期)时,它会显示无效日期,等等:我正在使用datetimepicker获取日期 $( "#start_date" ).datetimepicker({ dateFormat : 'yy-mm-dd', timeFormat: 'hh:mm:ss', defaultDateTime: "+1w", showSecond: true,

我在转换方面遇到了一些麻烦。当
警报(第一个日期)和警报(结束日期)
时,它会显示无效日期,等等:我正在使用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=""/>
var first_date=新日期($('start_date').val());
var last_date=新日期($('end_date').val();
变量差异=(最后一个日期-第一个日期);
var一天=1000*60*60*24;
变量天数=数学轮(无差异/一天);

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'))

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'))

创建新日期时,
$(“#start_date”).val()
的值是多少?如果放置一个
T
而不是空格,它将起作用-因此:
2012-01-01T06:16:16
并且在结尾可能需要一个
Z
,这可以解释时区偏移。您似乎正在使用jQuery插件-。但是您正在使用
onClose
事件,该事件仅在jQuery日期选择器中可用,而在插件的日期时间选择器选项中不可用。@Bruno是的,我在使用jQuery。@skdnewbie是一个插件,而它是jQueryUI的一部分。你能检查链接并告诉我你是否在使用该插件,如果不是,请告诉我哪个插件。当你创建
新日期时,
$(“#start_date”).val()的值是多少,它将起作用-因此:
2012-01-01T06:16:16
,您可能需要也可能不需要在末尾使用一个
Z
,它可以解释时区偏移量。您似乎正在使用jQuery插件-。但是您正在使用
onClose
事件,该事件仅在jQuery日期选择器中可用,而在插件的日期时间选择器选项中不可用。@Bruno是的,我在使用jQuery。@skdnewbie是一个插件,而它是jQueryUI的一部分。你能检查一下这个链接,告诉我你是否在使用这个插件,如果不是,是哪个插件吗。