[removed] subtracting Time and getting its number of minutes

前端 未结 4 1222
不知归路
不知归路 2021-01-02 17:50

For Example:

StartTime = \'00:10\';
EndTIme = \'01:20\';

These variables are string

Question: How can I Subtract them and returning

4条回答
  •  借酒劲吻你
    2021-01-02 17:54

    If you're going to be doing a lot of date/time manipulation, it's worth checking out date.js.

    However, if you're just trying to solve this one problem, here's an algorithm off the top of my head.

    (1)Parse start/end values to get hours and minutes, (2)Convert hours to minutes, (3)Subtract

    function DifferenceInMinutes(start, end) {
        var totalMinutes = function(value) {
            var match = (/(\d{1,2}):(\d{1,2})/g).exec(value);
            return (Number(match[1]) * 60) + Number(match[2]);
        }    
        return totalMinutes(end) - totalMinutes(start);
    }
    

提交回复
热议问题