Javascript 确定两个日期之间经过的时间

Javascript 确定两个日期之间经过的时间,javascript,Javascript,我试图使用javascript确定两个日期之间经过的时间。举个例子:我在2008年1月5日凌晨3点戒烟,从我戒烟到现在已经有多少年、几个月、几个小时了 所以我的想法是: 退出日期 获取当前日期 转换为时间毫秒 找出区别 使用差异创建新日期 提取自该日期起的年、月等 嗯,它的行为很奇怪,我不知道为什么。有什么见解吗 //create custom test date var d1 = new Date(2012, 8, 28, 13, 14, 0, 0); //create current dat

我试图使用javascript确定两个日期之间经过的时间。举个例子:我在2008年1月5日凌晨3点戒烟,从我戒烟到现在已经有多少年、几个月、几个小时了

所以我的想法是:

退出日期 获取当前日期 转换为时间毫秒 找出区别 使用差异创建新日期 提取自该日期起的年、月等 嗯,它的行为很奇怪,我不知道为什么。有什么见解吗

//create custom test date
var d1 = new Date(2012, 8, 28, 13, 14, 0, 0);
//create current date
var d2 = new Date();
//get date times (ms)
var d1Time = (d1.getTime());
var d2Time = (d2.getTime());
//calculate the difference in date times
var diff = d2 - d1;
//create a new date using the time differences (starts at Jan 1, 1970)
var dDiff = new Date();
dDiff.setTime(diff);
//chop off 1970 and get year, month, day, and hour
var years = dDiff.getFullYear() - 1970;
var months = dDiff.getMonth();
var days = dDiff.getDate();
var hours = dDiff.getHours();

您可以在上看到它的作用。

为什么不做数学运算来计算这些值呢?当您执行dDiff.setTimediff时,您将在日期中输入什么;这对你来说毫无意义。这将为您提供与纪元不同的日期。

更改部分代码可能会解决您的问题

但您必须根据这些值来操作这些值,因为它们可能为负值。

日期表示特定的时间点,而不是两个日期之间的时间跨度。 您正在通过设置自unix纪元以来已禁用的dDiff毫秒来创建新日期

一旦你有了这些数据,你应该通过分割来提取你需要的信息


我可以推荐您看一下吗?

这不准确,因为它没有考虑闰日。除此之外,它工作正常,我看不出任何问题。时差大约为6.5天。考虑到时区和0是1月1日这一事实,我看到的值与预期值相同

准确的解决办法是

将时差转换为天 减去指定日期之后经过的闰年数 将剩余的天数除以365得到天数 在不考虑闰日的情况下,创建一个包含每月天数的数组,并循环遍历已用月份,减去已完成月份的天数。迭代次数将是您的月份计数 剩下的就是你的天数 各种注释:

新日期2012年8月28日13月14日0月0日;如果你愿意的话,2012年9月28日13:14:00不是8月吗 由于使用夏令时的做法,new Date0返回的值不是常量。 dDiff.getMonth;1月返回0,2月返回1等。 1970年1月1日的开始日期以1开始,所以差值应减去1。 我认为第二点是你的错误

根据您的算法,尝试以下操作:

// create date in UTC
//create custom test date
var dlocaltime = new Date(2012, 8, 28, 13, 14, 0, 0);
var d1 = new Date(dlocaltime.getUTCFullYear(),dlocaltime.getUTCMonth(), dlocaltime.getUTCDate(), dlocaltime.getUTCHours(),dlocaltime.getUTCMinutes(),dlocaltime.getUTCSeconds());
//create current date
var now = new Date();
var d2 = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
console.log(d1);
console.log(d2);
//get date times (ms)
var d1Time = (d1.getTime());
var d2Time = (d2.getTime());


//calculate the difference in date times
var diff = d2 - d1;
//create a new date using the time differences (starts at Jan 1, 1970)
var dDiff = new Date();
dDiff.setTime(diff);
//chop off 1970 and get year, month, day, and hour
var years = dDiff.getUTCFullYear() - 1970;
var months = dDiff.getUTCMonth();
var days = dDiff.getUTCDate()-1; // the date of new Date(0) begin with 1 
var hours = dDiff.getUTCHours();
var minutes = dDiff.getUTCMinutes();
var seconds = dDiff.getUTCSeconds();
console.log("Years:"+years);
console.log("months:"+months);
console.log("days:"+days);
console.log("hours:"+hours);
console.log("minutes:"+minutes);
console.log("seconds:"+seconds);

试着使用timestamp timestamp\u now-previous\u timestamp,然后将时间戳转换为日期。@Bill,如果这是他们想要实现的全部,那么可能会有点过分,但这是一个整洁的工具。我自己可以用。我明白你的意思。我希望能够确定几个月过去了,我猜为什么我认为创建一个新的日期对象会有所帮助。这需要我考虑月长30,31,28,29。看看momente.js-它可能会满足你的需要=哦,momente.js太棒了。非常感谢,我现在明白了。我也在努力提取几个月过去的时间。使用diff值创建新的日期对象显然不起作用。谢谢
// create date in UTC
//create custom test date
var dlocaltime = new Date(2012, 8, 28, 13, 14, 0, 0);
var d1 = new Date(dlocaltime.getUTCFullYear(),dlocaltime.getUTCMonth(), dlocaltime.getUTCDate(), dlocaltime.getUTCHours(),dlocaltime.getUTCMinutes(),dlocaltime.getUTCSeconds());
//create current date
var now = new Date();
var d2 = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
console.log(d1);
console.log(d2);
//get date times (ms)
var d1Time = (d1.getTime());
var d2Time = (d2.getTime());


//calculate the difference in date times
var diff = d2 - d1;
//create a new date using the time differences (starts at Jan 1, 1970)
var dDiff = new Date();
dDiff.setTime(diff);
//chop off 1970 and get year, month, day, and hour
var years = dDiff.getUTCFullYear() - 1970;
var months = dDiff.getUTCMonth();
var days = dDiff.getUTCDate()-1; // the date of new Date(0) begin with 1 
var hours = dDiff.getUTCHours();
var minutes = dDiff.getUTCMinutes();
var seconds = dDiff.getUTCSeconds();
console.log("Years:"+years);
console.log("months:"+months);
console.log("days:"+days);
console.log("hours:"+hours);
console.log("minutes:"+minutes);
console.log("seconds:"+seconds);