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');












share|improve this question



















  • 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 for shift()).
    – 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














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');












share|improve this question



















  • 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 for shift()).
    – 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












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');












share|improve this question


















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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, 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










  • Thank you @Jeto
    – Mr world wide
    Nov 10 at 14:14












  • 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 for shift()).
    – 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












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/






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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/






    share|improve this answer


























      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/






      share|improve this answer
























        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/






        share|improve this answer














        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/







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 5:09

























        answered Nov 10 at 14:19









        front_end_dev

        1,310411




        1,310411



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto