programing

모멘트를 사용하여 날짜 목록에서 최소 날짜 또는 최대 날짜를 얻는 방법?

minimums 2023. 2. 22. 21:39
반응형

모멘트를 사용하여 날짜 목록에서 최소 날짜 또는 최대 날짜를 얻는 방법?

handle Click 함수로 지정된 날짜 목록에서 최대 날짜를 원합니다.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)

커스텀 컴파터를 사용하여 정렬한 후 첫 번째 컴파터를 선택합니다(또는 마지막으로 테스트합니다).

   array.sort(function(d1, d2) {
       return moment(d1).isBefore(moment(d2));
        });

언급URL : https://stackoverflow.com/questions/46502405/how-to-get-min-or-max-dates-from-a-list-of-dates-using-moment-js

반응형