添加两个格式为“HH +:MM:SS”的时间戳

时间:2013-08-03 12:35:27

标签: javascript jquery timestamp

所以基本上我有两个时间戳字符串,我想添加:

a = "00:10:12";
aParts = a.split(/:/);  

b = "00:30:34";
bParts = b.split(/:/);  

time1 = 3600000 * parseInt(aParts[0]) + 60000 * parseInt(aParts[1]) + 1000 * parseInt(aParts[2]);
time2 = 3600000 * parseInt(bParts[0]) + 60000 * parseInt(bParts[1]) + 1000 * parseInt(bParts[2]);

dateTime = time1 + time2;

hours = parseInt(dateTime/3600000);
dateTime = parseInt(dateTime%3600000);

minutes = parseInt(dateTime/60000);
dateTime = parseInt(dateTime%60000);

seconds = parseInt(dateTime/1000);

newTime = addLeadingZeros(hours,2) + ':' + addLeadingZeros(minutes,2) + ':' + addLeadingZeros(seconds,2);

// returns correct "00:40:46"

function addLeadingZeros (n, length){
   var str = (n > 0 ? n : -n) + "";
   var zeros = "";
   for (var i = length - str.length; i > 0; i--)
     zeros += "0";
   zeros += str;
   return n >= 0 ? zeros : "-" + zeros;
}

在写这个问题时,我设法提出上面的代码:-)以某种方式工作 - 是一种添加两个字符串时间戳的正确方法还是有更好的方法?

忘记提及 - 我确实尝试将两个字符串转换为Date对象并使用.getTime()添加两个日期时间 - 但这给了我错误的约会时间。

1 个答案:

答案 0 :(得分:0)

您的代码没有任何明显错误,但请务必在使用parseInt时设置radix

  

基数

     

一个整数,表示要解析的值的基数。总是   指定此参数以消除读者混淆   并保证可预测的行为。不同的实现   在未指定基数时产生不同的结果。

执行您所描述的任务没有标准方法。

这是我过去使用过的一个例子。

的Javascript

/*jslint maxerr: 50, indent: 4, browser: true, devel: true */

(function () {
    "use strict";

    function zeroPad(num) {
        var str = num.toString();

        if (num < 2) {
            str = "0" + str;
        }

        return str;
    }

    function addTimes() {
        if (!arguments.length) {
            throw new SyntaxError("No arguments provided.");
        }

        var total = {
                hours: 0,
                minutes: 0,
                seconds: 0
            },
            argIndex,
            argLength,
            time,
            parts,
            part,
            partIndex,
            temp;

        for (argIndex = 0, argLength = arguments.length; argIndex < argLength; argIndex += 1) {
            time = arguments[argIndex];
            if (typeof time !== "string") {
                throw new TypeError("Argument must be a string.");
            }

            parts = time.split(":");
            if (parts.length !== 3) {
                throw new SyntaxError("Argument is incorrectly formatted.");
            }

            for (partIndex = 0; partIndex < 3; partIndex += 1) {
                part = parts[partIndex];
                if (partIndex < 2) {
                    if (part === "" || !/^\d*$/.test(part)) {
                        throw new SyntaxError("Argument is incorrectly formatted.");
                    }

                    parts[partIndex] = parseInt(part, 10);
                } else {
                    if (part === "" || !/^\d*\.?\d+$/.test(part)) {
                        throw new SyntaxError("Argument is incorrectly formatted.");
                    }

                    parts[partIndex] = parseFloat(part);
                }
            }

            temp = (parts[2] + total.seconds);
            total.seconds = temp % 60;
            temp = (parts[1] + total.minutes) + (temp - total.seconds) / 60;
            total.minutes = temp % 60;
            total.hours = (parts[0] + total.hours) + (temp - total.minutes) / 60;
        }

        return zeroPad(total.hours) + ":" + zeroPad(total.minutes) + ":" + zeroPad(total.seconds);
    }

    var a = "00:10:12",
        b = "00:30:34",
        c = "10:40:40";

    console.log(addTimes(a, b, c));
}());

输出

11:21:26 

jsfiddle