Reputation: 1364
I want to Math.min a few Date values (e.g. var1.getTime(), var2.getTime(), and var3.getTime()). However, before I can use .getTime() on them, I need to make sure they exist (so the script doesn't crash).
If one of the variables doesn't exist I still want to get the Math.min of the other variables that exist.
Any clues?
Upvotes: 0
Views: 2039
Reputation: 36511
If you store your date variables in an array, you can use filter
to remove non-Date
objects and pass that to Math.min.apply
or Math.max.apply
const dates = [var1, var2, var3];
const min = Math.min(...dates.filter(date => date instanceof Date));
Or non-ECMAScript 6:
var dates = [var1, var2, var3];
var min = Math.min.apply(null, dates.filter(function(date) {
return date instanceof Date;
}));
Upvotes: 3