jQuery 如何计算持续时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15493521/
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-08-26 15:11:30  来源:igfitidea点击:

How do I calculate a duration time?

javascriptjqueryjquery-mobile

提问by C_Major

I am developing a web-based application to capture start time and end time from system date-time but my main problem is that I don't know how can I get the duration time, between start and end time for downtime.

我正在开发一个基于 Web 的应用程序来从系统日期时间捕获开始时间和结束时间,但我的主要问题是我不知道如何获得停机时间的开始时间和结束时间之间的持续时间。

 //Function to get current start time
  var startTime = setInterval(function () { start() }, 1000);

  function start() {
    var startDate = new Date();
    var timeStart = startDate.toLocaleTimeString();
  $("#setCount").html(timeStart);
 }

回答by Sergio

Do you mean this:

你的意思是:

var date1 = new Date();
var date2 = new Date();
var diff = date2 - date1; //milliseconds interval

upd

更新

check JSFiddle

检查JSFiddle

回答by C_Major

Update:

更新:

If you want to display the time difference to user, Serigo'smethodwill do it. But if it is for any development purposes, below functions will make your life easy.

如果您想向用户显示时差,可以这样做。但如果是出于任何开发目的,以下功能将使您的生活变得轻松。Serigo'smethod



Just wanted to let you know about this console utility functions.

只是想让您了解此控制台实用程序功能。

Put this line in top of your app initialization code console.time('appLifeTime');

将此行放在应用初始化代码的顶部 console.time('appLifeTime');

Put this one in where ever you feel that your app ends. console.timeEnd('appLifeTime');

把这个放在你觉得你的应用程序结束的地方。 console.timeEnd('appLifeTime');

console.time('appLifeTime');

setTimeout(function delay(){
  console.timeEnd('appLifeTime');
}, 1500);

The above code piece will print, appLifeTime: 1500.583ms.

上面的代码片段将打印,appLifeTime: 1500.583ms.

AFAIK, console.time & console.timeEndworks in firefox(with firebug) & webkit browsers(chrome, safari).

AFAIK,console.time & console.timeEnd适用于 firefox(带 firebug)和 webkit 浏览器(chrome、safari)。

回答by kamituel

To simply measure time elapsed, use Date.getTime()which outputs current time in milliseconds since unix epoch.

要简单地测量经过的时间,请使用Date.getTime()which 输出自 unix epoch 以来以毫秒为单位的当前时间。

You can substract one millis value from another to get the duration.

您可以从另一个值中减去一个毫秒值以获得持续时间。

Example:

例子:

var startTime = new Date().getTime();

setTimeout(function () {
  var endTime = new Date().getTime();
  console.log("duration [ms] = " + (endTime-startTime));
}, 1500);

Output would be, of course: duration [ms] = 1500(or couple ms less or more).

输出当然是:(duration [ms] = 1500或少或多几毫秒)。

回答by Igor Sukharev

I have this function to show the time distance between two dates:

我有这个功能来显示两个日期之间的时间距离:

export const timeDistance = (date1, date2) => {
  let distance = Math.abs(date1 - date2);
  const hours = Math.floor(distance / 3600000);
  distance -= hours * 3600000;
  const minutes = Math.floor(distance / 60000);
  distance -= minutes * 60000;
  const seconds = Math.floor(distance / 1000);
  return `${hours}:${('0' + minutes).slice(-2)}:${('0' + seconds).slice(-2)}`;
};

The output in the format h:mm:sshours can grow arbitrary.

h:mm:ss小时格式的输出可以任意增长。

回答by Gajotres

Use this nifty jQuery plugin: http://timeago.yarp.com/

使用这个漂亮的 jQuery 插件:http: //timeago.yarp.com/

Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago"). Download, view the examples, and enjoy.

You opened this page about a minute ago. (This will update every minute. Wait for it.)

This page was last modified 13 days ago.

Ryan was born 34 years ago.

Timeago 是一个 jQuery 插件,可以轻松支持自动更新模糊时间戳(例如“4 分钟前”或“大约 1 天前”)。下载、查看示例并享受。

您大约在一分钟前打开了此页面。(这将每分钟更新一次。等待它。)

此页面最后修改于 13 天前。

瑞安出生于 34 年前。