Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have created a javascript that will display start and stop time when the user choose the option from dropdown menu, please some one help me how to calculate the time difference of stop and start time.(我创建了一个JavaScript,当用户从下拉菜单中选择选项时,它将显示开始和结束时间,请有人帮我如何计算停止和开始时间的时差。)

here is below code(这是下面的代码)

<script>
var myTime = new Date().toLocaleString(navigator.language, {
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit'
});


function getValue(data) {



  var selectedText = $("#ddselect").find("option:selected").text();
  if (selectedText != "None") {



    var display = document.getElementById("display");
    var newRow = display.insertRow(display.rows.length);

    var cell1 = newRow.insertCell(0);
    cell1.innerHTML = myTime;

    var cell2 = newRow.insertCell(1);
    cell2.innerHTML = selectedText;

    /* stop time */
    var stopTime = new Date().toLocaleString(navigator.language, {
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    });

    var cell3 = newRow.insertCell(2);
    cell3.innerHTML = stopTime;

    var cell4 = newRow.insertCell(3);
    cell4.innerHTML = stopTime - myTime;

  }

}
</script>
  ask by sanjiv kumar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
209 views
Welcome To Ask or Share your Answers For Others

1 Answer

One way would be to use getTime() .(一种方法是使用getTime() 。)

cell4.innerHTML = new Date(stopTime.getTime() - myTime.getTime()); Should do the trick.(应该做到的。)

A more complete solution (for a larger application) would be to use a lib such as momentJS .(一个更完整的解决方案(对于更大的应用程序)将是使用lib,例如momentJS 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...