I'd suggest passing an anonymous function to the sort() method:
var dates = ['10-Jan-2013','12-Dec-2013','1-Sep-2013','15-Sep-2013'],
orderedDates = dates.sort(function(a,b){
return Date.parse(a) > Date.parse(b);
});
console.log(orderedDates); // ["10-Jan-2013", "1-Sep-2013", "15-Sep-2013", "12-Dec-2013"]
var dates = ['10-Jan-2013', '12-Dec-2013', '1-Sep-2013', '15-Sep-2013'],
orderedDates = dates.sort(function(a, b) {
return Date.parse(a) > Date.parse(b);
});
console.log(orderedDates);
JS Fiddle demo.
Note the use of an array ['10-Jan-2013','12-Dec-2013','1-Sep-2013','15-Sep-2013'] of quoted date-strings.
The above will give you an array of dates, listed from earliest to latest; if you want only the earliest, then use orderedDates[0].
A revised approach, to show only the earliest date – as requested in the question – is the following:
var dates = ['10-Jan-2013', '12-Dec-2013', '1-Sep-2013', '15-Sep-2013'],
earliest = dates.reduce(function (pre, cur) {
return Date.parse(pre) > Date.parse(cur) ? cur : pre;
});
console.log(earliest); // 10-Jan-2013
var dates = ['10-Jan-2013', '12-Dec-2013', '1-Sep-2013', '15-Sep-2013'],
earliest = dates.reduce(function(pre, cur) {
return Date.parse(pre) > Date.parse(cur) ? cur : pre;
});
console.log(earliest);
JS Fiddle demo.
References:
Date.parse().Array.prototype.reduce().Array.prototype.sort().
I'd suggest passing an anonymous function to the sort() method:
var dates = ['10-Jan-2013','12-Dec-2013','1-Sep-2013','15-Sep-2013'],
orderedDates = dates.sort(function(a,b){
return Date.parse(a) > Date.parse(b);
});
console.log(orderedDates); // ["10-Jan-2013", "1-Sep-2013", "15-Sep-2013", "12-Dec-2013"]
var dates = ['10-Jan-2013', '12-Dec-2013', '1-Sep-2013', '15-Sep-2013'],
orderedDates = dates.sort(function(a, b) {
return Date.parse(a) > Date.parse(b);
});
console.log(orderedDates);
JS Fiddle demo.
Note the use of an array ['10-Jan-2013','12-Dec-2013','1-Sep-2013','15-Sep-2013'] of quoted date-strings.
The above will give you an array of dates, listed from earliest to latest; if you want only the earliest, then use orderedDates[0].
A revised approach, to show only the earliest date – as requested in the question – is the following:
var dates = ['10-Jan-2013', '12-Dec-2013', '1-Sep-2013', '15-Sep-2013'],
earliest = dates.reduce(function (pre, cur) {
return Date.parse(pre) > Date.parse(cur) ? cur : pre;
});
console.log(earliest); // 10-Jan-2013
var dates = ['10-Jan-2013', '12-Dec-2013', '1-Sep-2013', '15-Sep-2013'],
earliest = dates.reduce(function(pre, cur) {
return Date.parse(pre) > Date.parse(cur) ? cur : pre;
});
console.log(earliest);
JS Fiddle demo.
References:
Date.parse().Array.prototype.reduce().Array.prototype.sort().
This question is very old, but maybe you will find it helpful. It is going to give you the oldest date in the array.
const result = Math.min(...list.map((stringDate) => Date.parse(stringDate).getTime()))
It is returning number, so if you would love to use it as a date you have to use
new Date(result)
Sometimes the most basic approach is the best:
var dates = ["09/09/2009", "16/07/2010", "29/01/2001"];
var min = dates[0];
for(var i = 1; i < dates.length; i++) {
if (fDate(dates[i]) < fDate(min))
min = dates[i];
}
alert(min);
// create a proper Date object from the string
function fDate(s) {
var d = new Date();
s = s.split('/');
d.setFullYear(s[2]);
d.setMonth(s[1]);
d.setDate(s[0]);
return d;
}
The code I wrote for you above converts each string into a Date object, and then finds the minimum (the earliest date) from them. No string hacks, just straightforward date comparison. It returns the original string from the array.
var dateArray = ["09/09/1980","09/09/2009", "16/07/2010", "29/01/1990"];
var first = dateArray[0].split("/").reverse().join("-");
var arrayLength = dateArray.length;
for(var i=1; i< arrayLength; i++){
second = dateArray[i].split("/").reverse().join("-");
if (first > second){
first = second;
}
}
alert(first);
At first you should split your string, then sort your array and pick the first element from the sorted array:
let str = '2019-12-31T23:45:00.000-03:00/2020-01-01T10:30:00.000+06:00,2020-01-01T07:15:00.000+07:00/2019-12-31T16:00:00.000-10:00';
let dates = str.split(/[\,/]+/);
let sortedDates = dates.sort((a,b) =>{
a = a.substring(0, a.lastIndexOf('.'));
b = b.substring(0, b.lastIndexOf('.'));
return new Date(a) - new Date(b);
});
console.log(`earliest date is ${sortedDates[0]}`);
I've used substring function to get date to be parsed by .parse() method.
Start by splitting your string into an array of date strings and converting those to date objects:
var str = '2019-12-31T23:45:00.000-03:00/2020-01-01T10:30:00.000+06:00,2020-01-01T07:15:00.000+07:00/2019-12-31T16:00:00.000-10:00'
// Split on slashes and commas to create an array of dates.
var dates = str.split(/\/,/)
.map(s => new Date(s))
That will give you an array of date objects. You can then sort the array by date as converted to epoch milliseconds:
// Sort by epoch milliseconds of the date.
dates.sort(function(a, b) { return a.getTime() - b.getTime() })
Finally, take the first result, since the list will be sorted by date ascending:
var earliest = dates[0]
If you need to convert back to ISO format, use earliest.toISOString().
Code is tested with IE,FF,Chrome and works properly:
var dates=[];
dates.push(new Date("2011/06/25"))
dates.push(new Date("2011/06/26"))
dates.push(new Date("2011/06/27"))
dates.push(new Date("2011/06/28"))
var maxDate=new Date(Math.max.apply(null,dates));
var minDate=new Date(Math.min.apply(null,dates));
Something like:
var min = dates.reduce(function (a, b) { return a < b ? a : b; });
var max = dates.reduce(function (a, b) { return a > b ? a : b; });
Tested on Chrome 15.0.854.0 dev
A clean way to do it would be to convert each date to a Date() and take the max
ES6:
new Date(Math.max(...a.map(e => new Date(e.MeasureDate))));
JS:
new Date(Math.max.apply(null, a.map(function(e) {
return new Date(e.MeasureDate);
})));
where a is the array of objects.
What this does is map each of the objects in the array to a date created with the value of MeasureDate. This mapped array is then applied to the Math.max function to get the latest date and the result is converted to a date.
By mapping the string dates to JS Date objects, you end up using a solution like Min/Max of dates in an array?
--
A less clean solution would be to simply map the objects to the value of MeasureDate and sort the array of strings. This only works because of the particular date format you are using.
a.map(function(e) { return e.MeasureDate; }).sort().reverse()[0]
If performance is a concern, you may want to reduce the array to get the maximum instead of using sort and reverse.
Further to @Travis Heeter's answer, this returns the object that contains the latest date:
array.reduce((a, b) => (a.MeasureDate > b.MeasureDate ? a : b));
A more robust solution perhaps might be convert the strings into Date objects every time. Could be noticeably slower if dealing with (very) large arrays:
array.reduce((a, b) => {
return new Date(a.MeasureDate) > new Date(b.MeasureDate) ? a : b;
});
I found _.min is reliable on moment instances. The following works:
var dates = _.map([date1,date2],function(date){return moment(date)});
var start = _.min(dates);
nevermind.
var earliest = new Date(Math.min.apply(null, dates))
see mdn Function.apply
Math.min(arg1,arg2,...argN) finds the lowest of N numeric arguments.
Because of how Function.prototype.apply works, Math.min.apply takes two arguments, a
this setting, which appears to be unneeded here, and a arg array.
The numeric representation of a date is the ms since 1970. A date coerced to a number will yield this value. A new date object is initialized to this lowest value, which is the same date as the earliest date but not the same object.
This is similar to @fardjad's approach, but does not need the underscore.js library for _.min., and is shortened by the use of func.apply, which should be common javascript to most recent browsers.