Javascript 在 Moment.js 中获取天数、小时数和分钟数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41875679/
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
Get the days, hours and minutes in Moment.js
提问by I am L
So This is my first time using Moment.js and I encountered the following problem, so I have this following dates:
所以这是我第一次使用 Moment.js,我遇到了以下问题,所以我有以下日期:
now: 2017-01-26T14:21:22+0000
expiration: 2017-01-29T17:24:22+0000
What I want to get is:
我想得到的是:
Day: 3
Hours: 3
Mins: 3
I tried the following code:
我尝试了以下代码:
const now = moment();
const exp = moment(expire_date);
console.log(expire_date);
days = exp.diff(now, 'days');
hours = exp.diff(now, 'hours') - (days * 24);
minutes = exp.diff(now, 'minutes') - ((days * 1440) + (hours * 24) * 60);
I know I did something wrong (maybe my calculation or I used the wrong method), but I can't figure out what it is.
我知道我做错了什么(可能是我的计算或我使用了错误的方法),但我无法弄清楚它是什么。
回答by gotomanners
MomentJS can calculate all that for you without you doing any logic.
MomentJS 可以为您计算所有这些,而无需您做任何逻辑。
- First find the difference between the two moments
- Express it as a Duration
- Then display whichever component
.days(),.hours()of the duration that you want.
- 首先找到两个时刻的区别
- 将其表示为持续时间
- 然后显示任何组件中
.days(),.hours()持续时间是你想要的。
Note: You can also express the entire duration .asDays(), .asHours()etc if you want.
注意:如果需要.asDays(),您还可以表达整个持续时间.asHours()等。
const now = moment("2017-01-26T14:21:22+0000");
const expiration = moment("2017-01-29T17:24:22+0000");
// get the difference between the moments
const diff = expiration.diff(now);
//express as a duration
const diffDuration = moment.duration(diff);
// display
console.log("Days:", diffDuration.days());
console.log("Hours:", diffDuration.hours());
console.log("Minutes:", diffDuration.minutes());
<script src="https://momentjs.com/downloads/moment.js"></script>
回答by alebianco
this will give you the right values and remove the headache of manual calculations
这将为您提供正确的值并消除手动计算的麻烦
let expiration = "2017-01-29T17:24:22+0000"
const now = moment();
const exp = moment(expiration);
console.log(exp.format());
days = exp.diff(now, 'days');
hours = exp.subtract(days, 'days').diff(now, 'hours');
minutes = exp.subtract(hours, 'hours').diff(now, 'minutes');
console.log(days, hours, minutes)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
note that the substractoperations will mutate the original expvalue, so don't go passing it around expecting it to be the original date
请注意,这些substract操作会改变原始exp值,因此不要期望它是原始日期而传递它
回答by Nedim Hozi?
Try my solution. I think that is it:
试试我的解决方案。我认为就是这样:
var now = moment("2017-01-26T14:21:22+0000");
var expiration = moment("2017-01-29T17:24:22+0000");
var minsAverage = expiration.diff(now, "minutes");
var min = parseInt(minsAverage % 60);
var hours = parseInt(minsAverage / 60);
var days = parseInt(hours / 24);
hours = hours - 24*days;
回答by damianfabian
For more information check the documentation https://momentjs.com/docs/#/displaying/difference/
有关更多信息,请查看文档https://momentjs.com/docs/#/displaying/difference/
var now = moment()
var exp = moment().add(1,'days')
var days = exp.diff(now, 'days')
var months = exp.diff(now, 'months')
var years = exp.diff(now, 'years', true) //float number
console.log(now,exp,days, months, years)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

