0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Emilio
  • 1,314
  • 2
  • 15
  • 39
  • 1
    Possible duplicate of [How might I find the largest number contained in a JavaScript array?](http://stackoverflow.com/questions/1379553/how-might-i-find-the-largest-number-contained-in-a-javascript-array) – Mike Cluck Sep 09 '16 at 17:38
  • Put your date values into an array then follow ^ question. – Mike Cluck Sep 09 '16 at 17:38
  • What's the behavior if none of them exist? – Scott Sauyet Sep 09 '16 at 17:43
  • @Mike C Yes, but how do you make sure they exist before you put them in the Array? Because, if one of them don't exist .getTime() will make the script crash. And how do you .getTime() them before you Math.min them ? – Emilio Sep 09 '16 at 17:50
  • @ScottSauyet The behavior should be that nothing happens. – Emilio Sep 09 '16 at 17:50
  • @Emilio The same way that you check for anything. Only add them to the array of `someVar instanceof Date`. – Mike Cluck Sep 09 '16 at 17:51
  • @Emilio: [Nothing at all?](https://www.youtube.com/watch?v=qf7rntVimhw) The code that gives you back the max/min Date or timestamp -- does it return nothing? `null`? Throw an exception? – Scott Sauyet Sep 09 '16 at 17:58
  • @ScottSauyet I mean, I'm fine with undefined or null. – Emilio Sep 09 '16 at 18:02
  • Have you got a code snippet to share so we can give you useful advice? – jwpfox Sep 09 '16 at 22:30

1 Answers1

3

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;
}));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • Yeah, but you still need to .getTime() them before you can Math.min them ? How do you do that? – Emilio Sep 09 '16 at 17:45
  • No you don't, dates are implicitly converted to numbers (their timestamp value) when mathematical operations are performed on them: `+new Date()` – Rob M. Sep 09 '16 at 17:46
  • @RobM. I see you updated the es6 code. Should we also update the non-es6 code or not? Thanks – Emilio Sep 09 '16 at 17:54
  • @Emilio, nope, es6 doesn't need `.apply` because of the spread operator, prior to es6 `.apply` was the way to achieve this – Rob M. Sep 09 '16 at 18:08
  • Great answer! I would just add a "new Date" to your answer in order to get back a date value. So, for non-es6, var dates = [var1, var2, var3]; var min = new Date(Math.min.apply(null, dates.filter(function(date) { return date instanceof Date; }))); – Emilio Sep 09 '16 at 19:50