3

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 earliest one with a for loop so the result will be

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

the language is javascript

gyula
  • 229
  • 3
  • 7
  • 12

5 Answers5

1

Sometimes the most basic approach is the best:

var dates = ["09/09/2009", "16/07/2010", "29/01/2001"];
var min = dates[0];
for(var i = 1; i < dates.length; i++) {
  if (fDate(dates[i]) < fDate(min))
    min = dates[i];
}

alert(min);

// create a proper Date object from the 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 I wrote for you above converts each string into a Date object, and then finds the minimum (the earliest date) from them. No string hacks, just straightforward date comparison. It returns the original string from the array.

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • It'd be faster to use `new Date('dd/mm/yyyy')` to create the Date objects (assuming you're running the JS in a locality that uses that format). – Blazemonger Jul 27 '15 at 13:38
  • Yes, a few nanoseconds. However, that format won't work with the Date object (but `mm/dd/yyyy` will) and you'd lose performance on string manipulation. Just saw your edit about locality - yeah, that would probably cause a lot of errors/wrong dates with different users, I'd rather not do it. – Shomz Jul 27 '15 at 13:41
  • Sorry, no. JavaScript will recognize whether the local user uses `mm/dd/yyyy` or `dd/mm/yyyy` formats and process the string accordingly. The only good reason to do it your way is if an international audience is expected. – Blazemonger Jul 27 '15 at 13:43
  • Hmmm, `new Date("29/01/2001")` fails for me, I don't think it can recognize the format automatically. Are you sure about that? Are both `new Date("29/01/2001")` and `new Date("01/29/2001")` working for you? – Shomz Jul 27 '15 at 13:43
  • How are your computer system preferences set up? Are you using a localized browser or an American one? My brief research on Wikipedia suggests that `29.01.2001` is your preferred local format anyway. – Blazemonger Jul 27 '15 at 13:46
  • It should be an American one. Yes, `29.01.2001.` seems natural to me, but not to the Date function. – Shomz Jul 27 '15 at 13:47
  • At least when you're parsing on an American system, this is wrong. Month is zero-based. I had to do d.setMonth(s[1] - 1); get get it to work. – Jamie Mar 31 '18 at 00:01
  • Also, I found this to be more concise: d.setFullYear(s[2], s[0] - 1, s[1]); – Jamie Mar 31 '18 at 00:30
1

   var dateArray = ["09/09/1980","09/09/2009", "16/07/2010", "29/01/1990"];
   var first = dateArray[0].split("/").reverse().join("-");
   var arrayLength = dateArray.length;
   for(var i=1; i< arrayLength; i++){
   second = dateArray[i].split("/").reverse().join("-");
     if (first > second){
        first = second;
     }
   }
alert(first);
Vijay
  • 924
  • 1
  • 12
  • 27
0

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);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
hsz
  • 148,279
  • 62
  • 259
  • 315
-1

The easiest way is to create dates object and the you can check grater/equal.. (date1 > date2)

Example to create date: Convert dd-mm-yyyy string to date

Community
  • 1
  • 1
Roy Shmuli
  • 4,979
  • 1
  • 24
  • 38
-2

You can get the earliest date by the below loop

var dateArray = ["09/09/2009", "16/07/2010", "29/01/2001"];
    alert(dateArray)//convert string to date object
var earliest = dateArray[0];
for(i=0; i<=dateArray.length; i++){
  var date1 = dateArray[i];
  if (date1 > earliest) {
     earliest = date1;

  }
}
alert(earliest)
user3270602
  • 696
  • 7
  • 15
  • This is the worst solution provided, will not work at all as you are just doing string comparisons, please do NOT post incorrect code as solution – Sayantan May 29 '18 at 02:59