moment.js差异日期格式 [英] moment.js diff date formatting
本文介绍了moment.js差异日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用moment.js,想计算两个时间戳之间的差异,然后格式化它们并在div中显示它们.
I'm using moment.js and want to calculate the difference between two timestamp,format them afterwards and display them in a div.
var diffTime = moment(1390310146.791877).diff( 1390309386.271075);
这给了我760秒,但我想像这样格式化它:
This gives me 760 seconds, but I want to format it like this:
(天,小时,分钟,秒),并且如果它们大于0,则仅显示天,小时和秒.
(days, hrs, mins, secs) and only show days, hours and seconds if they are higher than 0.
我该如何实现?
推荐答案
moment.duration应该使用
moment.duration should be used
var diffTime = moment('2016-06-13T00:00:00+08:00')
.diff( '2016-06-13T00:00:00+00:00');
var duration = moment.duration(diffTime);
var years = duration.years(),
days = duration.days(),
hrs = duration.hours(),
mins = duration.minutes(),
secs = duration.seconds();
var div = document.createElement('div');
div.innerHTML = years + ' years ' + days + ' days ' + hrs + ' hrs ' + mins + ' mins ' + secs + ' sec';
document.body.appendChild(div);
这篇关于moment.js差异日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文