简体   繁体   中英

datetime comparison and datetime different of javascript

i'm fail on simple datetime comparison and datetime different my input is from jquery UI datetimepicker,

var dt1 = $('#datetimepicker1').val(); //2012-08-12 13:49
var dt2 = $('#datetimepicker2').val(); //2012-08-14 14:21
if( (dt1!="") && (dt2!="") ){
    var d1 = Date.parse(dt1); // return NaN
    var dd1 = new Date(Date.parse(dt1));    // return Invalid Date
    var ddd1 = dt1.getTime() // added this, the entire script no functioning
    alert (dt1 +":"+ d1 + ":"+ dd1 +"\n"+ ddd1);
}

anyone know what i did wrong here? prefer solution of javascript.

Try this way as per the format provided in question 2012-08-12 13:49

To find out the difference between two dates in days:hours:minutes you can do by this,

Live Demo

function ConvertMilliSecondsToTimeSpan(t){
    var cd = 24 * 60 * 60 * 1000,
        ch = 60 * 60 * 1000,
        d = Math.floor(t / cd),
        h = '0' + Math.floor( (t - d * cd) / ch),
        m = '0' + Math.round( (t - d * cd - h * ch) / 60000);
    return [d, h.substr(-2), m.substr(-2)].join(':');
}

var dt1 = Date.parse("2012-08-12 13:49");
var dt2 = Date.parse("2012-08-11 14:21");

alert("Difference: Days:Hours:Minutes " + ConvertMilliSecondsToTimeSpan(Math.abs(dt2 - dt1)));
​
var dt1 = "2012-08-12 13:49";
var dt2 = "2012-08-14 14:21";
if( (dt1!="") && (dt2!="") ){
    var d1 = new Date(dt1.split(" ").join("T")); 
    var d2 = new Date(dt2.split(" ").join("T"));
    var diff = d2 - d1; // difference in milliseconds

    //console.log(d1, d2, diff/(60*60*24*1000));
    alert(d1);
    alert(d2);
    if(diff > 0){
      alert(d1 + " before " + d2);
    }else if(diff < 0){
      alert(d2 + " before " + d1);
    }else{
      alert(d2 + " before " + d1);
    }
    alert(Math.abs(diff/(60*60*24*1000)).toFixed(2) + " days");

}

Actually your code it look like true somehow but it isn't. When parsing from string, using Date.parse there is a format of string to be used. I tried to mention all. Try to pass strings of the below format.

The format is: YYYY-MM-DDTHH:mm:ss.sssZ. The 'Z' part denotes an optional time zone. Subforms are also possible:

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

example:

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

So Let apply to you question. This is working to me with the mentioned output

      //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
        }

Simple and clearn method, which produces no additional js Date objects or requires any parsing.

// get epoch of datepicker 1
var dt1 = $('#datetimepicker1').datepicker('getDate').getTime(); 
// get epoch of datepicker 2
var dt2 = $('#datetimepicker2').datepicker('getDate').getTime(); 
// ensure both evaluate to true
if(dt1 && dt2){
     if(dt1 < dt2){
        //dt1 is earlier
     }else if(dt1 > dt2){
        //dt1 is later
     }else{
        //dates are equal
     }
}

如果您发现自己正在做一个日期时间繁重的应用程序,则http://momentjs.com/会使用更加灵活的API来包装本机Date对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM