1

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

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() 添加两个日期时间 - 但这给了我一个错误的日期时间。

4

1 回答 1

0

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

基数

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

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

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

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 上

于 2013-08-03T14:33:35.610 回答