user2571510 user2571510 - 1 year ago 85
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.


<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 Source
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"));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download