Reputation: 631
How can I get the least and max date in an array using YYYY-mm-dd format? Here is a sample of my date values.
const data = [{
"date": "2012-10-21",
"value": 60
}, {
"date": "2012-10-22",
"value": 61
}, {
"date": "2012-10-23",
"value": 69
}, {
"date": "2012-10-24",
"value": 67
}]
console.log(data);
Upvotes: 0
Views: 1222
Reputation: 1
function checkDate(data,condition){
if(condition == 'max'){
var max = 0
data.map((item)=>{
if(new Date(item.date)>max){
max = new Date(item.date)
}
})
return max;
}else if(condition == 'least'){
var least = new Date()
data.map((item)=>{
if(new Date(item.date)<least){
least = new Date(item.date)
}
})
return least;
}
}
Upvotes: 0
Reputation: 50639
If you just want the dates you can use Math.min
and Math.max
if you map your array to dates using .map
:
const data = [{
"date": "2012-10-21",
"value": 60
}, {
"date": "2012-10-22",
"value": 61
}, {
"date": "2012-10-23",
"value": 69
}, {
"date": "2012-10-24",
"value": 67
}];
const dates = data.map(({date}) => new Date(date));
const minDate = new Date(Math.min(...dates));
const maxDate = new Date(Math.max(...dates));
console.log("min", minDate.toISOString().slice(0,10));
console.log("max", maxDate.toISOString().slice(0,10));
Alternatively, you could sort the array and use the first and last elements:
const data = [{
"date": "2012-10-21",
"value": 60
}, {
"date": "2012-10-22",
"value": 61
}, {
"date": "2012-10-23",
"value": 69
}, {
"date": "2012-10-24",
"value": 67
}];
const dates = data.map(({date}) => new Date(date));
const sortedDates = dates.sort((a, b) => a - b);
const minDate = sortedDates[0];
const maxDate = sortedDates[sortedDates.length-1];
console.log("min", minDate.toISOString().slice(0,10));
console.log("max", maxDate.toISOString().slice(0,10));
Upvotes: 2
Reputation: 8589
If this is the date format used, a simple .sort()
on the date property will work. The earliest date is the first element the array and the last date the last element.
This is the big advantage of using a date format ( like the ISO standard ) where the lexical storting by string value is the same as the logical sorting by date.
const data = [{
"date": "2012-10-21",
"value": 60
}, {
"date": "2012-10-22",
"value": 61
}, {
"date": "2012-10-23",
"value": 69
}, {
"date": "2012-10-24",
"value": 67
}, {
"date": "2012-10-22",
"value": 102
}];
const sorted_by_date = data.sort(( a, b ) => a.date.localeCompare( b.date ));
const earliest_date = sorted_by_date[ 0 ];
const latest_date = sorted_by_date[ sorted_by_date.length - 1 ];
console.log( earliest_date );
console.log( latest_date );
Upvotes: 1
Reputation: 386520
You could reduce the data and take just a string comparison.
const
data = [{ date: "2012-10-21", value: 60 }, { date: "2012-10-22", value: 61 }, { date: "2012-10-23", value: 69 }, { date: "2012-10-24", value: 67 }],
result = data.reduce((r, { date }) => {
if (!r) return { min: date, max: date };
if (r.min > date) r.min = date;
if (r.max < date) r.max = date;
return r;
}, undefined);
console.log(result);
Upvotes: 1
Reputation: 37755
You can use reduce and Date.parse
const data = [{"date": "2012-10-21","value": 60}, { "date": "2012-10-22","value": 61}, {"date": "2012-10-23","value": 69}, {"date": "2012-10-24","value": 67}]
let maxDate = data.reduce((op,inp)=>{
if(Date.parse(inp.date) > Date.parse(op.max)){
op.max = inp.date
}
if(Date.parse(inp.date) < Date.parse(op.max)){
op.least = inp.date
}
return op
},{least:data[0].date,max:data[0].date})
console.log(maxDate)
Upvotes: 2