Calculate timespan in JavaScript

前端 未结 7 2033
刺人心
刺人心 2021-01-01 21:48

I have a .net 2.0 ascx control with a start time and end time textboxes. The data is as follows:

txtStart.Text = 09/19/2008 07:00:00

txtEnd.Text = 09/19/200

7条回答
  •  無奈伤痛
    2021-01-01 21:54

    function stringToDate(string) {
            var matches;
        if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2}) (\d{2,2}):(\d{2,2}):(\d{2,2})$/)) {
           return new Date(matches[1], matches[2] - 1, matches[3], matches[4], matches[5], matches[6]);
        } else {
           return null;
        };
    }
    
        function getTimeSpan(ticks) {
            var d = new Date(ticks);
            return {
                hour: d.getUTCHours(), 
                minute: d.getMinutes(), 
                second: d.getSeconds()
            }
        }
    
        var beginDate = stringToDate('2008-09-19 07:14:00');
        var endDate = stringToDate('2008-09-19 17:35:00');
    
        var sp = getTimeSpan(endDate - beginDate);
        alert("timeuse:" + sp.hour + " hour " + sp.minute + " minute " + sp.second + " second ");
    

    you can use getUTCHours() instead Math.floor(n / 3600000);

提交回复
热议问题