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?
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
To compare two dates, just convert the dates to moment object and use the isSame() function. Compare if only day is the same. Check if months are equal. Check for year.
The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().
Moment JS allows displaying of date as per localization and in human readable format. You can use MomentJS inside a browser using the script method. It is also available with Node. js and can be installed using npm.
You can use moment.max function :
let moments = this.state.dates.map(d => moment(d)), maxDate = moment.max(moments)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With