[removed] subtracting Time and getting its number of minutes

前端 未结 4 1223
不知归路
不知归路 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);
    }
    
    0 讨论(0)
  • 2021-01-02 17:57
    var startTime = "0:10";
    var endTime = "1:20";
    
    var s = startTime.split(':');
    var e = endTime.split(':');
    
    var end = new Date(0, 0, 0, parseInt(e[1], 10), parseInt(e[0], 10), 0);
    var start = new Date(0, 0, 0, parseInt(s[1], 10), parseInt(s[0], 10), 0);
    
    var elapsedMs = end-start;
    var elapsedMinutes = elapsedMs / 1000 / 60;
    
    0 讨论(0)
  • 2021-01-02 17:58

    Make a function to parse a string like that into minutes:

    function parseTime(s) {
       var c = s.split(':');
       return parseInt(c[0]) * 60 + parseInt(c[1]);
    }
    

    Now you can parse the strings and just subtract:

    var minutes = parseTime(EndTIme) - parseTime(StartTime);
    
    0 讨论(0)
  • 2021-01-02 18:02

    dojo.date.difference is built for the task - just ask for a "minute" interval.

    Get the difference in a specific unit of time (e.g., number of months, weeks, days, etc.) between two dates, rounded to the nearest integer.

    Usage:

    var foo: Number (integer)=dojo.date.difference(date1: Date, date2: Date?, interval: String?);
    
    0 讨论(0)
提交回复
热议问题