Eugene Goldberg Eugene Goldberg - 1 year ago 125
Node.js Question

How to calculate max number using node.js?

In my node script I have the following function, which determines max date:

if (result.rows.length > 0) {
var maxDate = new Date( => o.REPORTED_DATE).reduce(function(a, b) {
return Math.max(a, Date.parse(b));

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?

Answer Source

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, => 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, => o.REPORTED_DATE)) * 1000
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download