如何使用 javascript/jquery 向时间字符串添加一秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12806059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:10:06  来源:igfitidea点击:

How to add one second to a time string with javascript/jquery

javascripttimerseconds

提问by Martignoni

I am trying to create a timer with Javascript but I don't know how to add one second to a time string.

我正在尝试使用 Javascript 创建一个计时器,但我不知道如何向时间字符串添加一秒。

Time string: 03:31:15

时间字符串:03:31:15

function updateWorked() {
        var time = $("#worked").html();
        ???
        $("#worked").html(wtime);
}

$(document).ready(function() {
    setInterval('updateWorked()', 1000);
});

What should I write in "???" to make this work?

我应该用“???”写什么 使这项工作?

回答by Ed Bayiates

Assuming you are using something like PHP to get the time string in the first place, and you can't keep track of the date/time as a number as suggested by Marc B, you can parse the string yourself like this:

假设您首先使用 PHP 之类的东西来获取时间字符串,并且您无法按照 Marc B 的建议将日期/时间作为数字跟踪,您可以像这样自己解析字符串:

var $worked = $("#worked");
var myTime = $worked.html();
var ss = myTime.split(":");
var dt = new Date();
dt.setHours(ss[0]);
dt.setMinutes(ss[1]);
dt.setSeconds(ss[2]);
var dt2 = new Date(dt.valueOf() + 1000);
var ts = dt2.toTimeString().split(" ")[0];
$worked.html(ts);

Edit: Working jsFiddle hereof this code.

编辑此代码的jsFiddle 中工作。

Here's the code with a timer: jsFiddle

这是带有计时器的代码:jsFiddle

回答by Pim

Below is an example on how to add a second to a time string. You can use the date object to print out the string in any format that you would like, in this example i'm just using the build in toTimeString method.

下面是一个关于如何向时间字符串添加秒的示例。您可以使用日期对象以您想要的任何格式打印出字符串,在本例中,我只是使用内置的 toTimeString 方法。

var timeString = "10/09/2012 14:41:08";
// start time
var startTime = new Date(timeString);
// prints 14:41:08 GMT-0400 (EDT)
console.log(startTime.toTimeString())
// add a second to the start time
startTime.setSeconds(startTime.getSeconds() + 1);
// prints 14:41:09 GMT-0400 (EDT)
console.log(startTime.toTimeString())

回答by saml

If you're trying to keep a counter in real time, you should use new Date()to get the time, and then format it:

如果您想实时保留计数器,则应使用new Date()获取时间,然后对其进行格式化:

function updateWorked() {
    var time = new Date(),
        wtime = formatDate(time);
    $("#worked").html(wtime);
}

However, if you're trying to keep a specific time, then you should up-scope a Date object and use that:

但是,如果您想保持特定时间,那么您应该扩大 Date 对象的范围并使用它:

var time = new Date(/* your starting time */);
function updateWorked() {
    time.setTime(time.getTime()+1000);
    var wtime = formatDate(time);
    $("#worked").html(wtime);
}

Also, you'd want to add a formatDatefunction:

此外,您还想添加一个formatDate函数:

function formatDate(date) {
  var hours = date.getHours().toString();
  if (hours.length < 2) hours = '0'+hours;
  var minutes = date.getMinutes().toString();
  if (minutes.length < 2) minutes = '0'+minutes;
  var seconds = date.getSeconds().toString();
  if (seconds.length < 2) seconds = '0'+seconds;
  return hours+':'+minutes+':'+seconds;
}

回答by Soarabh

Using mixture of jquery and javascript you can achieve this example.

使用 jquery 和 javascript 的混合,你可以实现这个例子。

I tired to achive what you looking for, first created a date object and get all the values of time, minute and second and then replaced the value. Please have a look at jsfiddle

我厌倦了实现您的目标,首先创建了一个日期对象并获取了时间、分钟和秒的所有值,然后替换了该值。请看一下jsfiddle

DEMO

演示

http://jsfiddle.net/saorabhkr/xtrpK/

http://jsfiddle.net/saorabhkr/xtrpK/