在日期列表中查找最早的日期 [英] Find the oldest date in a list of dates

查看:146
本文介绍了在日期列表中查找最早的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个帮助来实现JavaScript中的功能。该功能应该能够以这种格式 yyyy-mm-dd hh:mm:ss 从日期列表中查找最早的日期。所以函数将收到一个文本,并且在它们的行中应该找到最旧的日期条目,并选择与该日期相关联的文本。

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.

如果有这个问题的Java解决方案这可以工作,我将使用JavaScript来包含Java。

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

推荐答案

这是工作,假设 datelist 是字符串,每个日期都在自己的行上:

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

这篇关于在日期列表中查找最早的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆