请问Node.js如何计算两个时间的差?Moment.js ???
发布于 7 年前 作者 dpc761218914 17805 次浏览 来自 问答

如数据库里面存了时间A“2016-11-16 15:20” 和时间B “2016-11-16 16:50” ,那么如何计算时间B与A的差是 1.5小时???

18 回复

可以先把两个时间转换成Date对象,然后getTime()变成时间戳来计算,接下来就是毫秒、秒、分钟、小时这些进制运算了。

看moment的api…

@pauky 嗯嗯,按照这思路,我试试,谢谢了

@sunzhiguang ok····谢谢了

Difference 1.0.0+

moment().diff(Moment|String|Number|Date|Array); moment().diff(Moment|String|Number|Date|Array, String); moment().diff(Moment|String|Number|Date|Array, String, Boolean); To get the difference in milliseconds, use moment#diff like you would use moment#from.

var a = moment([2007, 0, 29]); var b = moment([2007, 0, 28]); a.diff(b) // 86400000 To get the difference in another unit of measurement, pass that measurement as the second argument.

var a = moment([2007, 0, 29]); var b = moment([2007, 0, 28]); a.diff(b, ‘days’) // 1 The supported measurements are years, months, weeks, days, hours, minutes, and seconds. For ease of development, the singular forms are supported as of 2.0.0. Units of measurement other than milliseconds are available in version 1.1.1.

By default, moment#diff will return a number rounded towards zero (down for positive, up for negative). If you want a floating point number, pass true as the third argument. Before 2.0.0, moment#diff returned a number rounded to nearest, not a rounded number rounded towards zero.

var a = moment([2008, 6]); var b = moment([2007, 0]); a.diff(b, ‘years’); // 1 a.diff(b, ‘years’, true); // 1.5 If the moment is earlier than the moment you are passing to moment.fn.diff, the return value will be negative.

var a = moment(); var b = moment().add(1, ‘seconds’); a.diff(b) // -1000 b.diff(a) // 1000 An easy way to think of this is by replacing .diff( with a minus operator.

      // a < b

a.diff(b) // a - b < 0 b.diff(a) // b - a > 0 Month and year diffs

moment#diff has some special handling for month and year diffs. It is optimized to ensure that two months with the same date are always a whole number apart.

So Jan 15 to Feb 15 should be exactly 1 month.

Feb 28 to Mar 28 should be exactly 1 month.

Feb 28 2011 to Feb 28 2012 should be exactly 1 year.

See more discussion on the month and year diffs here

This change to month and year diffs was made in 2.0.0. As of version 2.9.0 diff also support quarter unit.

@KingTree 好像蛮6的,去官方文档详细看看。我目前先用一楼最原始的方式实现了

一楼的方法:docs[i].am_signin是存在数据库中类似:"2016-11-21"的字符串。

var am_signin = (new Date(docs[i].am_signin)); var am_signout = (new Date(docs[i].am_signout)); var am_signinH=am_signin.getHours(); var am_signinM=am_signin.getMinutes(); var am_signoutH=am_signout.getHours(); var am_signoutM=am_signout.getMinutes(); /计算上午的时间,取精度为2/ amTimes=(((am_signoutH60+am_signoutM)-(am_signinH60+am_signinM))/60).toFixed(2);

待我再尝试moment…

@dpc761218914

var am_signin = moment(docs[i].am_signin);
var am_signout = moment(docs[i].am_signout);
am_signin.diff(am_signout, ‘d’);       // 1
am_signin.diff(am_signout, ‘d’, true); // 1.5  这个很适合你。第三个参数为true,精确到小数

var millseconds = new Date(date1) - new Date(date2); 然后想算什么就算什么,另外,数据库自身一般都会带有时间格式化的函数

moment.duration(moment(‘2016-11-16 16:50’)-moment(‘2016-11-16 15:20’)).as(‘hours’)

@callten wow~蛮精炼~😁

来自酷炫的 CNodeMD

@heanxu 😄这不是很6…我文档没看全

来自酷炫的 CNodeMD

@sanrudongfeng 额。我用的mongodb,安卓客户端上传数据。直接存字符串了。。date类型好长。😭

来自酷炫的 CNodeMD

@KingTree 👍👍👍

来自酷炫的 CNodeMD

function timeAgo(val){
    let nowDate = new Date();
    let replyDate = new Date(val);
    let diffSeconds = (nowDate.getTime()-Number(replyDate.getTime()))/1000;
    let years = 365*24*60*60;
    let months = 30*24*60*60;
    let days = 24*60*60;
    let hours =  60*60;
    let minutes = 60;
    let seconds = 1;
    if(diffSeconds<seconds){
        return '1秒以前'
    }else if(seconds<=diffSeconds&&diffSeconds<minutes){
        return Math.floor(diffSeconds/seconds)+'秒前'
    }else if(minutes<=diffSeconds&&diffSeconds<hours){
        return Math.floor(diffSeconds/minutes)+'分钟前'
    }else if(hours<diffSeconds&&diffSeconds<days){
        return Math.floor(diffSeconds/hours)+'小时前'
    }else if(days<diffSeconds&&diffSeconds<months){
        return Math.floor(diffSeconds/days)+'天前'
    }else if(months<diffSeconds&&diffSeconds<years){
        return Math.floor(diffSeconds/months)+'个月前'
    }else{
        return Math.floor(diffSeconds/years)+'年前'
    }
}

@dpc761218914 大哥,你这存的客户端时间吗?肯定要存服务器时间啊,人家给你个不准的时间怎么办,,,你居然存字符串了。。。

@sanrudongfeng 额,有道理。我想想~ 记错了,我是在服务端的生成的。。但我确实是存字符串呢。。。date好长。不好用,而且我只要日期呢。。。字符串有嘛缺陷吗?

来自酷炫的 CNodeMD

@dpc761218914 没啥缺陷,如果你能保证你Date.parse(str)一定是true的话。

回到顶部