admin管理员组文章数量:1336289
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
5 Answers
Reset to default 3The 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
}
本文标签:
版权声明:本文标题:javascript - ReactJS, How to get the oldest and newest date from array using moment.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742407142a2469053.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论