[英]how to calculate total hours in Javascript?
I need to calculate the total hours in a time span using JS, an example of the time span 我需要使用JS(一个时间跨度的示例)来计算一个时间跨度的总小时数
10:00 - 16:00
So the answer is: 6 hours, but how do I work that out in JS? 答案是:6小时,但是我如何在JS中解决呢?
//Using MomentJs var a = moment([10,00] , "HH:mm"); var b = moment([16,00] , "HH:mm"); console.log(b.diff(a, 'hours'))
<script src="https://momentjs.com/downloads/moment.min.js"></script>
Try following 尝试跟随
let dif = new Date("01/01/2018 " + "16:00").getHours() - new Date("01/01/2018 " + "10:00").getHours(); console.log(dif);
//Using Js var a = new Date("12 May, 2018 10:00"); var b = new Date("12 May, 2018 16:00"); var hours = Math.abs(b - a) / 36e5; console.log(hours)
//new Date("YYYY-MM-DDTHH:mm:ssZ")
var date1 = new Date("2015-03-25T12:00:00Z");
var date2 = new Date("2015-03-24T19:00:00Z");
var hours = Math.abs(date1.getHours() - date2.getHours());
console.log(hours);
var start = new Date();
var finish = new Date();
start = start.setHours(10);
finish = finish.setHours(16);
var totalHours = finish - start;
totalHours = totalHours/3600000; // milleseconds in one hour
console.log(totalHours);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.