Min/Max of Dates in an Array

Determining the minimum and maximum date in an array object

Sort the array by date and pick the first and the last ones :

const dates = [  { settledDate: "12-19-2018" },  { settledDate: "12-12-2018" },  { settledDate: "10-19-2018" },  { settledDate: "10-12-2018" }];
const sorted = dates.sort((a, b) => new Date(a.settledDate) - new Date(b.settledDate));
const minDate = sorted[0];const maxDate = sorted.reverse()[0];
console.log('maxDate : ', maxDate.settledDate);console.log('minDate : ', minDate.settledDate);

Get max and min dates from an array

1) Use map to extract the dates:

var dates = data.map(function(x) { return new Date(x[4]); })

2) Use Math.max / Math.min to get the highest / lowest dates:

var latest = new Date(Math.max.apply(null,dates));
var earliest = new Date(Math.min.apply(null,dates));

check min date and max date using javascript

EDIT: As pointed out by @RobG the dates are in the ISO format so there is no need for dates at all:

if ('2022-04-30' < '2022-05-30')
console.log('true')

How to get Max Date in an array?

you can do the following:

const dates = [
"2022-10-13T00:00:00.000",
"2023-10-14T00:00:00.000",
"2024-10-15T00:00:00.000",
"2020-10-16T00:00:00.000",
"2015-10-17T00:00:00.000",
"2028-10-18T00:00:00.000",
"2010-10-19T00:00:00.000",
];
const datesArray = dates.map((element) => new Date(element));

const maxDate = new Date(Math.max(...datesArray));

console.log("maxDate", maxDate);

How to find the min/max date javascript

Solution is of coarse here on SO:
https://stackoverflow.com/a/7143443/7158959
You are already using first suggestion from that link, you should just try second one. Flag in it as duplicate.

var dates = [];

var fecha1 = "2020-05-05 15:00:00";var fecha2 = "2020-05-05 16:00:00";var fecha3 = "2020-05-06 13:00:00"; dates.push(fecha1); dates.push(fecha2); dates.push(fecha3);
console.log(dates);
var min = dates.reduce(function (a, b) { return a < b ? a : b; }); var max = dates.reduce(function (a, b) { return a > b ? a : b; });console.log(min);console.log(max);

How to get max and min dates from array using javascript/jquery?

Use Array#sort with custom sort function and get the last(max) and first(min) values.

data = ["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];
var sorted = data.slice() // copy the array for keeping original array with order // sort by parsing them to date .sort(function(a, b) { return new Date(a) - new Date(b); });
// get the first and last valuesconsole.log( 'max :', sorted.pop(), 'min :', sorted.shift());

How can I find the maximum and minimum date in an array?

<?php

$date_arr=array(0=>'20-05-2015',1=>'02-01-2015',2=>'30-03-2015');

usort($date_arr, function($a, $b) {
$dateTimestamp1 = strtotime($a);
$dateTimestamp2 = strtotime($b);

return $dateTimestamp1 < $dateTimestamp2 ? -1: 1;
});

echo 'Min: ' . $date_arr[0];
echo '<br/>';
echo 'Max: ' . $date_arr[count($date_arr) - 1];

?>


Related Topics



Leave a reply



Submit