admin管理员组文章数量:1415145
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
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
Share Improve this question asked Jul 27, 2015 at 11:57 gyulagyula 2573 gold badges7 silver badges12 bronze badges 2- stackoverflow./questions/492994/… – Prasad Commented Jul 27, 2015 at 12:00
- Reverse the strings, to a string sorting, take the first (or last) element. – Sirko Commented Jul 27, 2015 at 12:01
5 Answers
Reset to default 1Sometimes 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 parison. It returns the original string from the array.
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);
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./ajax/libs/moment.js/2.10.3/moment.min.js"></script>
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
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)
本文标签: javascripthow can I compare dates in array to find the earliest oneStack Overflow
版权声明:本文标题:javascript - how can I compare dates in array to find the earliest one? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745207756a2647720.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论