获取两个日期之间的时差,以秒为单位 [英] Get time difference between two dates in seconds

查看:167
本文介绍了获取两个日期之间的时差,以秒为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在几秒钟内得到两个日期之间的差异。逻辑将是这样的:




  • 设置一个初始日期,现在是这样;

  • 设置最终日期将是初始日期加上未来几秒钟的数量(例如,假设为15)

  • 获得这两者之间的差异(秒数)



我用日期来做它的原因是因为最后的日期/时间取决于一些其他变量,它永远不会相同关于用户做了多快的事情),而且我也存储其他事情的初始日期。



我一直在尝试这样的东西:

  var _initial = new Date(),
_initial = _initial.setDate(_initial.getDate()),
_final = new Date (_初始);
_final = _final.setDate(_final.getDate()+ 15/1000 * 60);

var dif = Math.round((_ final - _initial)/(1000 * 60));

事情是我从来没有得到正确的区别。我尝试除以 24 * 60 ,这会让我失秒,但我从来没有这样做。那么我的逻辑怎么了?我可能会做一些愚蠢的错误,因为它已经很晚了,但是让我烦恼,我不能让它工作:)

解决方案

代码



  var startDate = new Date(); 
//你的操作
var endDate = new Date();
var seconds =(endDate.getTime() - startDate.getTime())/ 1000;

甚至更简单(endDate - startDate)/ 1000



解释说明



您需要为 Date 对象调用 getTime()方法,然后简单地减去它们并除以1000(因为它最初是以毫秒为单位)。另外,当您调用 getDate()方法时,您实际上将该月的日期作为1到31之间的整数(不是基于0的),而不是从调用 getTime()方法获得的时代,代表1970年1月1日以来的毫秒数,00:00






Rant



根据你的日期您可能需要投资整合图书馆,例如 date.js moment.js ,这使得开发人员更容易,但这只是个人偏好的问题。



例如,在 moment.js 中,我们会code> moment1.diff(moment2,seconds)这是美丽的。






< h3>这个答案有用的文件


I'm trying to get a difference between two dates in seconds. The logic would be like this :

  • set an initial date which would be now;
  • set a final date which would be the initial date plus some amount of seconds in future ( let's say 15 for instance )
  • get the difference between those two ( the amount of seconds )

The reason why I'm doing it it with dates it's because the final date / time depends on some other variables and it's never the same ( it depends on how fast a user does something ) and I also store the initial date for other things.

I've been trying something like this :

var _initial = new Date(),
    _initial = _initial.setDate(_initial.getDate()),
    _final = new Date(_initial);
    _final = _final.setDate(_final.getDate() + 15 / 1000 * 60);

var dif = Math.round((_final - _initial) / (1000 * 60));

The thing is that I never get the right difference. I tried dividing by 24 * 60 which would leave me with the seconds, but I never get it right. So what is it wrong with my logic ? I might be making some stupid mistake as it's quite late, but it bothers me that I cannot get it to work :)

解决方案

The Code

var startDate = new Date();
// Do your operations
var endDate   = new Date();
var seconds = (endDate.getTime() - startDate.getTime()) / 1000;

Or even simpler (endDate - startDate) / 1000 as pointed out in the comments unless you're using typescript.


The explanation

You need to call the getTime() method for the Date objects, and then simply subtract them and divide by 1000 (since it's originally in milliseconds). As an extra, when you're calling the getDate() method, you're in fact getting the day of the month as an integer between 1 and 31 (not zero based) as opposed to the epoch time you'd get from calling the getTime() method, representing the number of milliseconds since January 1st 1970, 00:00


Rant

Depending on what your date related operations are, you might want to invest in integrating a library such as date.js or moment.js which make things so much easier for the developer, but that's just a matter of personal preference.

For example in moment.js we would do moment1.diff(moment2, "seconds") which is beautiful.


Useful docs for this answer

这篇关于获取两个日期之间的时差,以秒为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆