如何计算jQuery剩余的时间?

时间:2016-02-09 14:40:59

标签: javascript jquery

我有这个脚本:

CODE JS:

    var completeD = start.format("YYYY/MM/DD HH:mm");
    var dt = new Date(completeD);
    $("#time").val(normalize(dt.getHours()) + ":" + normalize(dt.getMinutes()));
    var star_H=$("#time").val();
    console.log("star_h",star_H);  //04:00--after execution

    var completeF = end.format("YYYY/MM/DD HH:mm");
    var dtf = new Date(completeF);
    $("#duration").val(normalize(dtf.getHours()) + ":" + normalize(dtf.getMinutes()));
    var end_H=$("#duration").val();
    console.log("end_H",end_H); //04:30--after execution
    var result=new Date(end_H - star_H);

    console.log(result);   // ERROR:INVALID DATA--here should be 00:30 min

我希望在决赛期间,在事件的最初期间找到...

我在意图代码中看得太清楚了:)

请告诉我如何更好地完成这项工作?

提前致谢!

修改

   function normalize(n) {
            if (n < 10) {
                n = "0" + n;
            }
            return n;
        }

1 个答案:

答案 0 :(得分:0)

你要减去字符串。

var completeD = start.format("YYYY/MM/DD HH:mm");
var dt = new Date(completeD); //<--- use this
$("#time").val(normalize(dt.getHours()) + ":" + normalize(dt.getMinutes()));
var star_H=$("#time").val();
console.log("star_h",star_H);  //04:00--after execution

var completeF = end.format("YYYY/MM/DD HH:mm");
var dtf = new Date(completeF); //<--- use this
$("#duration").val(normalize(dtf.getHours()) + ":" + normalize(dtf.getMinutes()));
var end_H=$("#duration").val();
console.log("end_H",end_H); //04:30--after execution

// try this:
var result=new Date(dtf - dt);