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

前端 未结 5 1644
故里飘歌
故里飘歌 2021-01-21 00:41

I have a variable called dateArray with dates in it for example

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

and I want to find the earlies

5条回答
  •  无人及你
    2021-01-21 01:29

    You can try with momentjs library:

    var dateArray = ["09/09/2009", "16/07/2010", "29/01/2001"],
        format = 'DD/MM/YYYY',
        minDate = moment(dateArray[0], format),
        minDateKey = 0;
    
    for (var i = 1; i < dateArray.length; i++) {
      var date = moment(dateArray[i], format);
      if (minDate > date) {
        minDate = date;
        minDateKey = i;
      }
    }
    
    alert(minDateKey);

提交回复
热议问题