Min/Max of dates in an array
up vote
0
down vote
favorite
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
var sorted = expenseDates.slice()
.sort(function(a, b)
return new Date(a) - new Date(b);
);
console.log(sorted.pop()+ '--max');
console.log(sorted.shift()+ '--min');
javascript arrays date
add a comment |
up vote
0
down vote
favorite
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
var sorted = expenseDates.slice()
.sort(function(a, b)
return new Date(a) - new Date(b);
);
console.log(sorted.pop()+ '--max');
console.log(sorted.shift()+ '--min');
javascript arrays date
2
Posting an image of your code or data is kind-of annoying because it's not possible to copy-paste in order to test it. Stackoverflow has ample facilities for rendering code directly in the question.
– Pointy
Nov 10 at 14:09
1
You're sorting your dates in ascending order, sopop()
will retrieve the max one, not the min one (other way around forshift()
).
– Jeto
Nov 10 at 14:11
@Pointy updated and found solution thanks,
– Mr world wide
Nov 10 at 14:13
Thank you @Jeto
– Mr world wide
Nov 10 at 14:14
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
var sorted = expenseDates.slice()
.sort(function(a, b)
return new Date(a) - new Date(b);
);
console.log(sorted.pop()+ '--max');
console.log(sorted.shift()+ '--min');
javascript arrays date
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
var sorted = expenseDates.slice()
.sort(function(a, b)
return new Date(a) - new Date(b);
);
console.log(sorted.pop()+ '--max');
console.log(sorted.shift()+ '--min');
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
var sorted = expenseDates.slice()
.sort(function(a, b)
return new Date(a) - new Date(b);
);
console.log(sorted.pop()+ '--max');
console.log(sorted.shift()+ '--min');
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
var sorted = expenseDates.slice()
.sort(function(a, b)
return new Date(a) - new Date(b);
);
console.log(sorted.pop()+ '--max');
console.log(sorted.shift()+ '--min');
javascript arrays date
javascript arrays date
edited Nov 10 at 14:42
asked Nov 10 at 14:04
Mr world wide
1,44021339
1,44021339
2
Posting an image of your code or data is kind-of annoying because it's not possible to copy-paste in order to test it. Stackoverflow has ample facilities for rendering code directly in the question.
– Pointy
Nov 10 at 14:09
1
You're sorting your dates in ascending order, sopop()
will retrieve the max one, not the min one (other way around forshift()
).
– Jeto
Nov 10 at 14:11
@Pointy updated and found solution thanks,
– Mr world wide
Nov 10 at 14:13
Thank you @Jeto
– Mr world wide
Nov 10 at 14:14
add a comment |
2
Posting an image of your code or data is kind-of annoying because it's not possible to copy-paste in order to test it. Stackoverflow has ample facilities for rendering code directly in the question.
– Pointy
Nov 10 at 14:09
1
You're sorting your dates in ascending order, sopop()
will retrieve the max one, not the min one (other way around forshift()
).
– Jeto
Nov 10 at 14:11
@Pointy updated and found solution thanks,
– Mr world wide
Nov 10 at 14:13
Thank you @Jeto
– Mr world wide
Nov 10 at 14:14
2
2
Posting an image of your code or data is kind-of annoying because it's not possible to copy-paste in order to test it. Stackoverflow has ample facilities for rendering code directly in the question.
– Pointy
Nov 10 at 14:09
Posting an image of your code or data is kind-of annoying because it's not possible to copy-paste in order to test it. Stackoverflow has ample facilities for rendering code directly in the question.
– Pointy
Nov 10 at 14:09
1
1
You're sorting your dates in ascending order, so
pop()
will retrieve the max one, not the min one (other way around for shift()
).– Jeto
Nov 10 at 14:11
You're sorting your dates in ascending order, so
pop()
will retrieve the max one, not the min one (other way around for shift()
).– Jeto
Nov 10 at 14:11
@Pointy updated and found solution thanks,
– Mr world wide
Nov 10 at 14:13
@Pointy updated and found solution thanks,
– Mr world wide
Nov 10 at 14:13
Thank you @Jeto
– Mr world wide
Nov 10 at 14:14
Thank you @Jeto
– Mr world wide
Nov 10 at 14:14
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You don't have convert it into date object as the date are in YYYY-MM-DD
format which itself is in sorted order by year => month => day
. So you just have to compare the input string as localCompare
. First index is minimum date while the last index is maximum date
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
expenseDates = expenseDates.sort(function(a, b)
return a.localeCompare(b);
);
console.log('--min => ',expenseDates[0]);
console.log('--max => ', expenseDates[expenseDates.length -1]);
Working jsFiddle demo - https://jsfiddle.net/rpdon5cm/1/
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You don't have convert it into date object as the date are in YYYY-MM-DD
format which itself is in sorted order by year => month => day
. So you just have to compare the input string as localCompare
. First index is minimum date while the last index is maximum date
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
expenseDates = expenseDates.sort(function(a, b)
return a.localeCompare(b);
);
console.log('--min => ',expenseDates[0]);
console.log('--max => ', expenseDates[expenseDates.length -1]);
Working jsFiddle demo - https://jsfiddle.net/rpdon5cm/1/
add a comment |
up vote
2
down vote
accepted
You don't have convert it into date object as the date are in YYYY-MM-DD
format which itself is in sorted order by year => month => day
. So you just have to compare the input string as localCompare
. First index is minimum date while the last index is maximum date
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
expenseDates = expenseDates.sort(function(a, b)
return a.localeCompare(b);
);
console.log('--min => ',expenseDates[0]);
console.log('--max => ', expenseDates[expenseDates.length -1]);
Working jsFiddle demo - https://jsfiddle.net/rpdon5cm/1/
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You don't have convert it into date object as the date are in YYYY-MM-DD
format which itself is in sorted order by year => month => day
. So you just have to compare the input string as localCompare
. First index is minimum date while the last index is maximum date
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
expenseDates = expenseDates.sort(function(a, b)
return a.localeCompare(b);
);
console.log('--min => ',expenseDates[0]);
console.log('--max => ', expenseDates[expenseDates.length -1]);
Working jsFiddle demo - https://jsfiddle.net/rpdon5cm/1/
You don't have convert it into date object as the date are in YYYY-MM-DD
format which itself is in sorted order by year => month => day
. So you just have to compare the input string as localCompare
. First index is minimum date while the last index is maximum date
var expenseDates = ["2018-02-06","2018-11-08","2018-11-10","2017-05-02","2017-05-02","2018-11-01"];
expenseDates = expenseDates.sort(function(a, b)
return a.localeCompare(b);
);
console.log('--min => ',expenseDates[0]);
console.log('--max => ', expenseDates[expenseDates.length -1]);
Working jsFiddle demo - https://jsfiddle.net/rpdon5cm/1/
edited Nov 11 at 5:09
answered Nov 10 at 14:19
front_end_dev
1,310411
1,310411
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239740%2fmin-max-of-dates-in-an-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Posting an image of your code or data is kind-of annoying because it's not possible to copy-paste in order to test it. Stackoverflow has ample facilities for rendering code directly in the question.
– Pointy
Nov 10 at 14:09
1
You're sorting your dates in ascending order, so
pop()
will retrieve the max one, not the min one (other way around forshift()
).– Jeto
Nov 10 at 14:11
@Pointy updated and found solution thanks,
– Mr world wide
Nov 10 at 14:13
Thank you @Jeto
– Mr world wide
Nov 10 at 14:14