[英]how to get min or max dates from a list of dates using moment.js?
I want to have the max date from the list of dates given in the handleClick function. 我想从handleClick函数中给出的日期列表中获取最大日期。 How to find the max date from the list of dates using moment.js ? 如何使用moment.js从日期列表中找到最大日期?
I have the following code: 我有以下代码:
import React, {Component} from 'react';
import moment from 'moment';
class Getdate extends Component
{
constructor() {
super();
this.state = {
dates = []
}
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.state.dates = ['2017-11-12', '2017-10-22', '2015-01-10', '2018-01-01', '2014-10-10'];
console.log(this.state.dates);
}
render{
return (
<button onClick={this.handleClick}>Get Max Date</button>
)
}
}
export default Getdate
You can use moment.max function : 你可以使用moment.max函数:
let moments = this.state.dates.map(d => moment(d)),
maxDate = moment.max(moments)
Sort them with a custom compartor, then select the first one (or last, try it out); 使用自定义隔离专区对它们进行排序,然后选择第一个(或最后一个,尝试一下);
array.sort(function(d1, d2) {
return moment(d1).isBefore(moment(d2));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.