MomentJs获得“时间直到”有两个变量 [英] MomentJs get "time until" with two variables
问题描述
我想让moment.js向我展示一段特定日期之前的时间。我做到了这一点:
let date = 2017-03-27T12:00:00;
moment()。to(date);
这显示:10天内
。
我真的希望这能在10天和2小时内显示
或两个最高值。例如, 1年零5个月
, 4分30秒
。
有一种简单的方法吗?我目前正在研究一种复杂的方法来处理这个......
让年=时刻(日期).local()。 diff(moment(),'years');
let months =(moment(date).local()。diff(moment(),'months'));
let days =(moment(date).local()。diff(moment(),'days'));
let hours =(moment(date).local()。diff(moment(),'hours'));
let minutes =(moment(date).local()。diff(moment(),'minutes'));
let seconds =(moment(date).local()。diff(moment(),'seconds'));
//以上值每个返回总数,
//例如,这可能显示1年,14个月,435天等等。
//下面的数学应该这样说
// 1年,2个月,14天等等。
let yearsRemain = years;
让monthsRemain =月 - (年* 12);
让daysRemain = days - (Math.floor(months * 30));
让hoursRemain =小时 - (天* 24);
让minutesRemain =分钟 - (小时* 60);
让secondsRemain =秒 - (分钟* 60);
var dateArray = [
yearsRemain,
monthsRemain,
daysRemain,
hoursRemain,
minutesRemain,
secondsRemain
]
console.log(dateArray);
/ *
返回[1,1,3,23,16,46],例如
* /
这个问题就是天。由于一年中的日子和一个月中的日子各不相同,我希望片刻.js会帮助我。有没有更好的方法呢?
最终,我将能够遍历数组并找到两个最大的值来显示我的喜好。
您可以使用 moment.duration
计算 dateArray
值。持续时间 years()
, months()
, days()
, hours()
, minutes()
, seconds()
getters。
这是一个实例:
让date ='2017-03-27T12:00:00'; var dur = moment.duration(moment(date).diff(moment())); l et yearsRemain = dur.years(); let monthsRemain = dur.months(); let daysRemain = dur.days(); let hoursRemain = dur.hours(); let minutesRemain = dur.minutes(); let secondsRemain = dur。 seconds(); var dateArray = [yearsRemain,monthsRemain,daysRemain,hoursRemain,minutesRemain,secondsRemain] console.log(dateArray);
< script src =// cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js\">< ; / script>
您可以使用 moment-duration-format 插件,以自定义格式显示持续时间。该插件有 模板
选项,允许您自定义格式。
let date ='2017 -03-27T12:00:00';让dur = moment.duration(moment(date).diff(moment())); console.log(dur.format()); console.log(dur.format(' M [月和] d [天]'));
< script src =https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js>< / script>< script src =https: //cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js\"></script>
$p $ p>
请注意,时刻已经 relativeTime
, relativeTimeRounding
和 relativeTimeThreshold
,可让您自定义时刻显示相对时间的方式(这样您就可以更改到
输出)。
I am trying to get moment.js to show me time until a specific date. I have that done doing this:
let date = 2017-03-27T12:00:00;
moment().to(date);
This shows: "in 10 days
".
I'd really like this to show "in 10 days and 2 hours
" or the two highest values. For example, 1 year and 5 months
, 4 minutes and 30 seconds
.
Is there a simple way to do this? I am currently working on a complicated method to handle this...
let years = moment(date).local().diff(moment(), 'years');
let months = (moment(date).local().diff(moment(), 'months'));
let days = (moment(date).local().diff(moment(), 'days'));
let hours = (moment(date).local().diff(moment(), 'hours'));
let minutes = (moment(date).local().diff(moment(), 'minutes'));
let seconds = (moment(date).local().diff(moment(), 'seconds'));
//The above values return total number each,
//For example, this could show 1 year, 14 months, 435 days, etc.
//The math below is supposed to make this say
//1 year, 2 months, 14 days, etc.
let yearsRemain = years;
let monthsRemain = months - (years *12);
let daysRemain = days - (Math.floor(months * 30));
let hoursRemain = hours - (days * 24);
let minutesRemain = minutes - (hours * 60);
let secondsRemain = seconds - (minutes * 60);
var dateArray = [
yearsRemain,
monthsRemain,
daysRemain,
hoursRemain,
minutesRemain,
secondsRemain
]
console.log(dateArray);
/*
returns [1, 1, 3, 23, 16, 46] for example
*/
the problem with this is when it comes to days. Since days in a year and days in a month vary, I was hoping moment.js would help me out. Is there a better way of doing this?
Eventually, I will be able to iterate through the array and find the two largest values to display how I'd like.
You can use moment.duration
to calculate the dateArray
value. Duration has years()
, months()
, days()
, hours()
, minutes()
, seconds()
getters.
Here a live example:
let date = '2017-03-27T12:00:00';
var dur = moment.duration( moment(date).diff(moment()) );
let yearsRemain = dur.years();
let monthsRemain = dur.months();
let daysRemain = dur.days();
let hoursRemain = dur.hours();
let minutesRemain = dur.minutes();
let secondsRemain = dur.seconds();
var dateArray = [
yearsRemain,
monthsRemain,
daysRemain,
hoursRemain,
minutesRemain,
secondsRemain
]
console.log(dateArray);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
You can use moment-duration-format plug-in to show duration in a custom format. The plugin has a template
option that lets you customize the format.
let date = '2017-03-27T12:00:00';
let dur = moment.duration( moment(date).diff(moment()) );
console.log( dur.format() );
console.log( dur.format('M [months and] d [days]') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
Note that moments has relativeTime
, relativeTimeRounding
and relativeTimeThreshold
that lets you customize how moment shows relative time (so you can change to
output).
这篇关于MomentJs获得“时间直到”有两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!