最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactJS, How to get the oldest and newest date from array using moment.js - Stack Overflow

programmeradmin0浏览0评论

I have this array:

DatosFechas = ["2020-04-29", "2020-04-19", "2020-04-07", "2020-04-30", "2020-04-30", "2018-01-22"]

And I would like to get the oldest or the newest date from a given range.

Ths function that trying to get work:

function ObtenerMaxMinRangoFechas(DatosFechas) {
    let moments = DatosFechas.map(x => moment(x)),
    maxDate = moment.max(moments)
    return maxDate

}

Thx

My date array is longer, just for the purpose of this posting it´s reduced.

This is the error:

Is not giving me the maxdate.

I have this array:

DatosFechas = ["2020-04-29", "2020-04-19", "2020-04-07", "2020-04-30", "2020-04-30", "2018-01-22"]

And I would like to get the oldest or the newest date from a given range.

Ths function that trying to get work:

function ObtenerMaxMinRangoFechas(DatosFechas) {
    let moments = DatosFechas.map(x => moment(x)),
    maxDate = moment.max(moments)
    return maxDate

}

Thx

My date array is longer, just for the purpose of this posting it´s reduced.

This is the error:

Is not giving me the maxdate.

Share Improve this question edited May 3, 2020 at 14:14 Kentron.dna asked May 3, 2020 at 14:08 Kentron.dnaKentron.dna 1933 silver badges12 bronze badges 2
  • 1 What is the problem that you are facing? – Utsav Patel Commented May 3, 2020 at 14:11
  • I updated the resulta that i am obtaning. Is not giving me the max date from the array. – Kentron.dna Commented May 3, 2020 at 14:17
Add a ment  | 

5 Answers 5

Reset to default 3

The returned maxDate variable is actually a Moment object. You can simply format it to get the desired result like:

const DatosFechas = ["2020-04-29", "2020-04-19", "2020-04-07", "2020-04-30", "2020-04-30", "2018-01-22"]
function ObtenerMaxMinRangoFechas(DatosFechas) {
    let moments = DatosFechas.map(x => moment(x)),
    maxDate = moment.max(moments)
    return maxDate.format('YYYY-MM-DD')
}

console.log( ObtenerMaxMinRangoFechas(DatosFechas) )
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.25.1/moment.min.js"></script>

Sort the dates array and grab the last one.

DatosFechas = ["2020-04-29", "2020-04-19", "2020-04-07", "2020-04-30", "2020-04-30", "2018-01-22"]
const sortedDates = DatosFechas.sort((a, b) => moment(a) - moment(b));
console.log('NEWEST Date',sortedDates[sortedDates.length-1])
console.log('OLDEST Date',sortedDates[0])

Note: You don't really need moment for your case. You can use js inbuilt Date feature. See here


You are doing great, you have done right. Just problem is that you have to formant your return moment object instead of returning it as an object.

Use it-

function ObtenerMaxMinRangoFechas(DatosFechas) {
    const moments = DatosFechas.map(x => moment(x));
    const maxDate = moment.max(moments);
    return maxDate.format('YYYY-MM-DD');
}

In what format do you want to return the first and last date? Here is an example with an array of the first and last string:

let dates = ["2020-04-29", "2020-04-19", "2020-04-07", "2020-04-30", "2020-04-30", "2018-01-22"]

const getFirstAndLastDate = (dates) => {
  dates = dates.sort()
  return [dates[0], dates[dates.length - 1]]
}

console.log(getFirstAndLastDate(dates))

Moment.max seems to be returning an moment object rather than a string in the format you want. to get the string format, you'll need to call format and pass the format you want, like this

moment.format('YYYY-MM-DD')  

let's put this together,

function ObtenerMaxMinRangoFechas(DatosFechas) {
    let moments = DatosFechas.map(x => moment(x)),
    maxDate = moment.max(moments).format('YYYY-MM-DD');
    return maxDate
}
发布评论

评论列表(0)

  1. 暂无评论