【问题标题】:Add two timestamps of format "HH+:MM:SS"添加两个格式为“HH+:MM:SS”的时间戳
【发布时间】:2013-08-04 16:05:25
【问题描述】:

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

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

【问题讨论】:

  • 你为什么使用常规时间格式,为什么不使用以毫秒为单位返回时间的new Date().getTime();
  • 忘了提及 - 我确实尝试将两个字符串转换为 Date 对象并使用 .getTime() 添加两个日期时间 - 但这给了我一个错误的日期时间。
  • 当然会,你要添加两个不同的时间(包括日期),如果你想把它们加在一起,你可以过滤掉其中一个的日期例如:dateTime = time1 + time2%216000000; /* returns the milliseconds of the time part only (1000*60*60*60).*/跨度>

标签: javascript jquery timestamp


【解决方案1】:

您的代码没有什么明显错误,但使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 2017-05-28
    • 2018-09-23
    相关资源
    最近更新 更多