user2571510 user2571510 - 7 months ago 20
Javascript Question

jQuery / moment.js: show newest / most recent value of input fields (dates)

I have a HTML form with multiple input fields that all have different dates as values (format: yyyy-mm-dd).

How can I get the newest, i.e. the most recent date out of these ?
I am using moment.js as well if that helps here.

My fields are standard input fields with the values being calculated based on previous selections.

Example:

<input type="text" class="milestone" id="ms1" name="ms1" value="2013-09-01" readonly />
<input type="text" class="milestone" id="ms2" name="ms2" value="2013-10-05" readonly />
<input type="text" class="milestone" id="ms3" name="ms3" value="2013-11-07" readonly />


So in this case my result should be "2013-11-07" as the most recent date out of the above.

Many thanks for any help with this, Tim.

Answer
var max=0;
var lastdate=0;

$("input[type='text']").each(function() {
    var Day = parseInt(moment($(this).val()).format("YYYYMMDD"));
    if (max <= Day)
        max = Day;
    lastdate = parseInt(moment($(this).val()).format("D"));
}); 

console.log(lastdate);