javascript - how can I compare dates in array to find the earliest one? -


i have variable called datearray dates in example

["09/09/2009", "16/07/2010", "29/01/2001"] 

and want find earliest 1 loop result be

"29/01/2001" - or datearray[2] 

the language javascript

sometimes basic approach best:

var dates = ["09/09/2009", "16/07/2010", "29/01/2001"];  var min = dates[0];  for(var = 1; < dates.length; i++) {    if (fdate(dates[i]) < fdate(min))      min = dates[i];  }    alert(min);    // create proper date object string  function fdate(s) {    var d = new date();    s = s.split('/');    d.setfullyear(s[2]);    d.setmonth(s[1]);    d.setdate(s[0]);    return d;  }

the code wrote above converts each string date object, , finds minimum (the earliest date) them. no string hacks, straightforward date comparison. returns original string array.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -