javascript 如何在Javascript中计算自午夜以来的毫秒数

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

How to calculate ms since midnight in Javascript

javascripttime

提问by Olga

What is the best way to calculate the time passed since (last) midnight in ms?

计算自(最后)午夜以来经过的时间(以毫秒为单位)的最佳方法是什么?

回答by RobG

A bunch of answers so here another:

一堆答案,所以这里是另一个:

var d = new Date(), e = new Date(d);
var msSinceMidnight = e - d.setHours(0,0,0,0);

As a function:

作为一个函数:

function getMsSinceMidnight(d) {
  var e = new Date(d);
  return d - e.setHours(0,0,0,0);
}

alert(getMsSinceMidnight(new Date()));

回答by Niet the Dark Absol

Create a new date using the current day/month/year, and get the difference.

使用当前日/月/年创建一个新日期,并获取差异。

var now = new Date(),
    then = new Date(
        now.getFullYear(),
        now.getMonth(),
        now.getDate(),
        0,0,0),
    diff = now.getTime() - then.getTime(); // difference in milliseconds

回答by Dan

Many answers except RobG's (recommended answer), Kolink'sand Lai'sare wrong here

除了RobG 的(推荐答案)KolinkLai之外许多答案在这里都是错误的

Let's look closer together

让我们一起仔细看看



First mistake

第一个错误

OptimusCrimeand Andrew D.answers:

OptimusCrimeAndrew D.回答:

As Malasugested, if the daylight saving correction was applied the nearest midnight, we get incorrect value. Let's debug:

正如Mala所说,如果在最近的午夜应用夏令时校正,我们会得到错误的值。让我们调试:

  1. Suppose it's last Sunday of March
  2. The time is fixed at 2 am.
  3. If we see 10 am on the clock, there's actually 11 hours passed from midnight
  4. But instead we count 10 * 60 * 60 * 1000ms
  5. The trick is played when midnight happens in different DST state then current
  1. 假设它是三月的最后一个星期日
  2. 时间固定在凌晨2点。
  3. 如果我们看到时钟上是上午 10 点,那么实际上距离午夜已经过去了 11 个小时
  4. 但相反,我们计算10 * 60 * 60 * 1000ms
  5. 当午夜发生在不同的 DST 状态然后当前


Second mistake

第二个错误

kennebeck'sanswer:

肯尼贝克的回答:

As RobGwrote, the clock can tick if you get the system time twice. We can even appear in different dates sometimes. You can reproduce this in a loop:

正如RobG所写,如果您两次获得系统时间,时钟就会滴答作响。我们有时甚至会出现在不同的日期。您可以在循环中重现此内容:

for (var i = 0; true; i++) {
    if ((new Date()).getTime() - (new Date()).getTime()) {
        alert(i); // console.log(i);  // for me it's about a 1000
        break;
    }
}


Thirdis my personal pitfall you could possibly experience

第三是我个人的陷阱,你可能会遇到

Consider the following code:

考虑以下代码:

var d = new Date(),
    msSinceMidnight = d - d.setHours(0,0,0,0);

msSinceMidnightis always 0as the object is changed during computation before the substraction operation

msSinceMidnight总是0因为在减法运算之前的计算过程中对象发生了变化



At last, this code works:

最后,此代码有效:

var d = new Date(),
    msSinceMidnight = d.getTime() - d.setHours(0,0,0,0);

回答by kennebec

Simpler to write, if you don't mind creating two dates.

如果您不介意创建两个日期,则编写起来更简单。

var msSinceMidnight= new Date()-new Date().setHours(0,0,0,0);

回答by Andrew D.

var d=new Date();
// offset from midnight in Greenwich timezone
var msFromMidnightInGMT=d%86400000;
// offset from midnight in locale timezone
var msFromMidnightLocale=(d.getTime()-d.getTimezoneOffset()*60000)%86400000;

回答by Lai Xin Chu

var today = new Date();
var d = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0); 

var difference = today.getTime() - d.getTime();

回答by OptimusCrime

Seconds since midnight would simply be to display the time, but instead of using hours:minutes:seconds, everything is converted into seconds.

从午夜开始的秒数只是显示时间,而不是使用小时:分钟:秒,所有内容都转换为秒。

I think this should do it:

我认为应该这样做:

var now = new Date();    
var hours = now.getHours()*(60*60);
var minutes = now.getMinutes()*60;
var seconds = now.getSeconds();

var secSinceMidnight = hours+minutes+seconds;