如何使用moment.js从日期列表中获取最小或最大日期? [英] how to get min or max dates from a list of dates using moment.js?
本文介绍了如何使用moment.js从日期列表中获取最小或最大日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望从 handleClick 函数中给出的日期列表中获取最大日期。
如何使用 moment.js 从日期列表中找到最大日期?
I want to have the max date from the list of dates given in the handleClick function. How to find the max date from the list of dates using moment.js?
我有以下代码:
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
推荐答案
您可以使用 moment.max 功能:
let moments = this.state.dates.map(d => moment(d)),
maxDate = moment.max(moments)
这篇关于如何使用moment.js从日期列表中获取最小或最大日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文