Prabin Poudel Prabin Poudel - 25 days ago 9
Javascript Question

Get the largest value from two DOB input fields

I have following input field for DOB.

<div class="po.pependent">
<input type="text", class="zm-textbox dob-input-mask" placeholder="dd/mm/yyyy" />,
<input type="text", class="zm-textbox dob-input-mask" placeholder="dd/mm/yyyy"/></div>


So if enter dob 11/12/1999 and 11/12/1997 the function paymentForm.computeAge return 17 and 19.

Now in JS i am trying to get the value of these to Dob convert them and get the highest age among the two dob entered.

$(".po.pependent:visible input.zm-textbox.dob-input-mask:visible")
.each(function() {
paymentForm.computeAge($(this).val())
})


This
paymentForm.computeAge
return the calculated age from the given dob date comparing with current system date.

Answer Source

You need to use Math.max. Below are 2 ways of doing it:

console.log(Math.max(17, 19));

var ageArray = [17, 19]
var max_of_array = Math.max.apply(Math, ageArray.map(Number));
console.log(max_of_array)