实际上,您的代码在某种程度上看起来像真的,但事实并非如此。从字符串解析时,使用 Date.parse 有一种要使用的字符串格式。我试图提及所有。尝试传递以下格式的字符串。
格式为:YYYY-MM-DDTHH:mm:ss.sssZ。“Z”部分表示可选时区。子表单也是可能的:
<ol>
<li>YYYY
<li>YYYY-MM
<li>YYYY-MM-DD
<li>THH:mm // example: 'T12:00'
<li>THH:mm:ss
<li>THH:mm:ss.sss
</ol>
例子:
var date1=Date.parse("2012-08-14");
var d = Date.parse("January 26, 2011 13:51:50");
var dd=var d = Date.parse('2011-01-26T13:51:50.417') ;
所以让我们来解决你的问题。这对我提到的输出有用
//var dt1 = $('#datetimepicker1').val(); //2012-08-12 13:49
// var dt2 = $('#datetimepicker2').val(); //2012-08-14 14:21
I realize that at least you are able to get dt1 and dt2 with values I assigned to date1 and date2, if so put the below code and see if you get the output .
var date1 ="2012-08-12 13:49";
var date2 ="2012-08-14 14:21";
var dt1=date1.split(" ").join("T");//2012-08-12T13:49
var dt2=date2.split(" ").join("T");//=>2012-08-14T14:21
if((dt1!="") && (dt2!="")){
alert(dt1);
var d1 = Date.parse(dt1); // return good output 1344768540000
var d2 = Date.parse(dt2); //return 1344943260000 Now you can compare d1 and d2 values
var dd1 = new Date(Date.parse(dt1)); // returned Sun Aug 12 2012 13:49:00 GMT+0300 (Egypt Standard Time)
var ddd1 = dd1.getTime(); // work fine 1344768540000 dt1 is a string can't work here
alert (dt1 +":"+ d1 + ":"+ dd1 +"\n"+ ddd1); // return 2012-08-12T13:49:1344768540000:Sun Aug 12 2012 13:49:00 GMT+0300 (Egypt Standard Time)
1344768540000
}