Reputation: 15554
In my node script I have the following function, which determines max date:
if (result.rows.length > 0) {
var maxDate = new Date(
result.rows.map(o => o.REPORTED_DATE).reduce(function(a, b) {
return Math.max(a, Date.parse(b));
})
);
}
The REPORTED_DATE
used to be a date, but now it changed to the unix epoch, which is a 10-digit number.
What is the proper syntax to determine max number instead of the date?
Upvotes: 0
Views: 192
Reputation: 150070
If REPORTED_DATE
is now a number then you should be able to simply remove the Date.parse()
call within your existing .reduce()
function:
return Math.max(a, b);
However, given that Math.max()
can handle any number of arguments you don't need to use .reduce()
, you can instead just use .apply()
to pass Math.max()
the array of numbers that you're producing with .map()
:
Math.max.apply(null, result.rows.map(o => o.REPORTED_DATE))
If the REPORTED_DATE
is a Unix-style 10-digit number then that is number of seconds, so you'd need to multiple it by 1000 to get milliseconds for conversion to a JavaScript Date
object. So putting that together in context:
if (result.rows.length > 0) {
var maxDate = new Date(
Math.max.apply(null, result.rows.map(o => o.REPORTED_DATE)) * 1000
);
}
Upvotes: 1