gpt4 book ai didi

javascript - 添加两个格式为 "HH+:MM:SS"的时间戳

转载 作者:行者123 更新时间:2023-11-28 08:58:54 24 4
gpt4 key购买 nike

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

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

最佳答案

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

radix

An integer that represents the radix of the value to parse. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.

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

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

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

关于javascript - 添加两个格式为 "HH+:MM:SS"的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18032925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com