0

I need a help in coming up with function in JavaScript. The function should be able to find the oldest date from the list of dates in this format yyyy-mm-dd hh:mm:ss. So the function would receive a text and among the lines it should find the oldest date entry and select text associated with that date.

Also if there is Java solution for this problem that could work to and I will use something to include Java inside JavaScript.

Tiago Sippert
  • 1,324
  • 7
  • 24
  • 33
edinvnode
  • 3,497
  • 7
  • 30
  • 53
  • 2
    What have you tried? Are you getting errors? Posting some code would be helpful. – km1 Aug 23 '12 at 00:40
  • 1
    You cannot (easily) include Java in JavaScript. What "something" would you have used? – Bergi Aug 23 '12 at 00:41
  • you can't do it in javascript, you need jquery – Dejan Marjanović Aug 23 '12 at 00:47
  • 4
    @webarto If you can't do it in JavaScript, then by definition you can't do it in jQuery either since jQuery is just a big ball of wibbly-wobbly, JavaScripty-wipty... stuff. – Niet the Dark Absol Aug 23 '12 at 00:51
  • @Kolink gonna save that for future reference – Dejan Marjanović Aug 23 '12 at 00:52
  • 1
    @Kolink that is quite possible the best description of jQuery I have ever read. – Lusitanian Aug 23 '12 at 00:55
  • In the olden days, there used to be some functionality of applets that let JS scripts and Java applets pass stuff back and forth. Not sure if it still exists, as i haven't played with applets for like 10 years. – cHao Aug 23 '12 at 00:56
  • @cHao It still exists and is still very useful when needed. I last used it about a year and a half ago. – Lusitanian Aug 23 '12 at 00:58
  • @webarto: You know that [this](http://i.stack.imgur.com/TdrW7.gif) is only a [meme](http://meta.stackexchange.com/a/19492/183280)? – Bergi Aug 23 '12 at 01:01
  • @Bergi: He's well aware. It's a running joke in the PHP chat. :) – cHao Aug 23 '12 at 01:15
  • Just so you know guys I was working on this one for a client and he didn't reply to me so I put this on hold. You CAN include java into javascript when you make scripts for iMacros. I found a code on their forum and I can post it here if anyone needs it. This question will be a good reference for my future works. – edinvnode Aug 31 '12 at 21:18

4 Answers4

3

Here is how you would do it...

  1. Iterate over the dates as an array, turning them into Unix epochs.
  2. Find the oldest with Math.min.apply(Math, arrayOfDates) or better yet Math.min(...arrayOfDates).
alex
  • 479,566
  • 201
  • 878
  • 984
3

This'd work, assuming datelist is the string and that each date is on its own line:

var oldest = (function() {var o = ":", c, a=datelist.split(/\r?\n/); while(c=a.shift()) o = o < c ? o : c; return o;})();

Breaking that down, here's how it works: It's basically creating a function, running it, then getting its return value. The function is like so:

var o = ":",
  // ":" comes after "9" in the character map, so it will be greater than any date
    c, a = datelist.split(/\r?\n/);
  // split on newlines, accomodating both \r and `\r\n` options.
while(c = a.shift()) {
  // basically loop through each date
    o = o < c ? o : c;
  // if the current oldest date is older than the one we're looking at, keep the old one
  // otherwise the new date is older so should be kept
}
return o;
  // return the result
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

You can convert all the date strings to Date objects, and then sort your list numerically and get the first item. Alternatively you can just apply Math.min on it to get the lowest value, if you don't need a sorted list.

var minDate = Math.min.apply(null, datestrings.map(Date));

Or, as your format does have leading zeroes, a simple string sort will do the same. To only search for a minimum string, you can use this:

var min = datestrings.reduce(function(min, cur) {
    return cur < min ? cur : min;
});
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • A sort is overkill unless the OP also wants the second oldest etc.. Sorting is a minimum of `O(n log n)` whereas scanning is `O(n)`. – Niet the Dark Absol Aug 23 '12 at 00:51
  • That's true, I already extended the answer with search code. `datestrings.sort()[0]` would just be the shortest to write :-) – Bergi Aug 23 '12 at 00:55
  • I think `Array.prototype.reduce` is fairly new to JS, and `sort` returns whether it succeeded or not so dereferencing it wouldn't work ;) – Niet the Dark Absol Aug 23 '12 at 00:56
0

A Wibbly-wobbly dollarsy-wollarsy example using jquery:

  $("body").append($("<div>").attr("id", "q")); //some gratuitous jquery
  var timelist = "2012-03-03 10:14:21 \r\n 2012-05-15 21:21:12\r\n 2012-07-01 10:19:19\r\n2012-02-11 21:21:12";
  var datelist = timelist.split("\r\n");
  var oldest = ":";
  $.each(datelist, function (a) {
    var trimmedThis = $.trim(this);
    if (trimmedThis < latest) oldest = trimmedThis;
  });
  $("#q").text(oldest); //just to beef up the $ count
wbit
  • 192
  • 1
  • 9