I need to calculate the total hours in a time span using JS, an example of the time span
10:00 - 16:00
So the answer is: 6 hours, but how do I work that out in 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);
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.