Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Return next date from an array of dates?

    Hi

    I have a list of dates in an array and am trying to code something that will return the next date in that list based on today's date.

    So, if my array includes the dates:

    09/09/2012
    14/09/2012
    18/09/2012
    03/10/2012

    Then it would return 14/09/2012 as the next date.

    I have attempted something below but am just getting a null value returned, am I close at all to what I need to do?!

    Thanks


    Code:
    var date1 = new Date(12,10,01);
    var date2 = new Date(12,10,15);
    var date3 = new Date(12,10,29);
    var date4 = new Date(12,11,12);
    
    var datearray = new Array(date1, date2, date3, date4);
    
    var today = new Date();
    
    datearray.sort();
    
    var nextLater = null;
    
    for (var i = 0; i < datearray.length; i++) {
      if (datearray[i] > today) {
        nextLater = datearray[i];
    	break;
      }
    }
    
    document.write(nextLater);

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    The year needs to be specified as yyyy, otherwise it would be 1900+year
    Code:
    var date1 = new Date(2012,10,01);
    var date2 = new Date(2012,10,15);
    var date3 = new Date(2012,10,29);
    var date4 = new Date(2012,11,12);
    Btw: Learn how to debug!

  • Users who have thanked devnull69 for this post:

    Buckster_uk (09-11-2012)

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Lightbulb

    As an alternative datearray creation method to avoid the used-one-time-only variables date1..date4,
    consider this...
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    
    var datearray = [];
    datearray.push(new Date(2012,10,01));  // Nov. 1, 2012  // format: (year,month,day) where month=0..11 for jan..dec
    datearray.push(new Date(2012,10,15));
    datearray.push(new Date(2012,10,29));
    datearray.push(new Date(2012,11,12));  // Dec. 12, 2012
    datearray.sort();
    
    var today = new Date();
    var nextLater = null;
    for (var i = 0; i < datearray.length; i++) {
      if (datearray[i] > today) { nextLater = datearray[i];	break; }
    }
    
    // document.write(nextLater);
    // alert(nextLater.toDateString());
    window.onload = function() {
      document.getElementById('dateInfo').innerHTML = nextLater;
    }
    </script>
    
    </head>
    <body>
    <div id='dateInfo' />
    </body>
    </html>

  • #4
    New Coder
    Join Date
    Apr 2004
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks to you both for the replies.

    jmrker I tried out your way and it seems to work but when adding in dates for 2013 I am getting some odd results.

    I added a couple of additional years and rather than returning Nov 1 2012 it is returning Feb 22 2013? It looks like it doesn't take the year into account and is just sorting based on the day and month. Any ideas?

    Many thanks.


    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    
    var datearray = [];
    datearray.push(new Date(2012,10,01));  // Nov. 1, 2012  // format: (year,month,day) where month=0..11 for jan..dec
    datearray.push(new Date(2012,10,15));
    datearray.push(new Date(2012,10,29));
    datearray.push(new Date(2012,11,12));  // Dec. 12, 2012
    datearray.push(new Date(2013,01,22));
    datearray.push(new Date(2013,03,20));
    datearray.sort();
    
    var today = new Date();
    var nextLater = null;
    for (var i = 0; i < datearray.length; i++) {
      if (datearray[i] > today) { nextLater = datearray[i];	break; }
    }
    
    // document.write(nextLater);
    // alert(nextLater.toDateString());
    window.onload = function() {
      document.getElementById('dateInfo').innerHTML = nextLater;
    }
    </script>
    
    </head>
    <body>
    <div id='dateInfo' />
    </body>
    </html>
    Last edited by Buckster_uk; 09-12-2012 at 09:58 AM.

  • #5
    New Coder
    Join Date
    Apr 2004
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Think it is working now - it appears as though when sorting the dates it puts the earliest month first (no matter what year it is from) so taking out the sort has worked.

    Thanks for your help.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    If there is a pattern to the dates (e.g. third Thursday in month) then you could generate the next upcoming date automatically. No need to list them in an array.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,529
    Thanks
    3
    Thanked 511 Times in 498 Posts
    Code:
    datearray.sort(function(a,b){ return a.getTime()-b.getTime(); });
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #8
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Lightbulb

    Quote Originally Posted by vwphillips View Post
    Code:
    datearray.sort(function(a,b){ return a.getTime()-b.getTime(); });
    I don't know if 'vwphillips' sort is better than the following,
    but I do believe it is in the sort where things are going wrong.
    Sort normally works with ascii chars, so a numerical sort function is needed for numbers.
    Since the getTime() function returns numbers, this should work.

    Note also, you do not need to sort if you initialize the array order by ascending dates anyway.

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    
    var datearray = [];
    datearray.push(new Date(2012,10,01));  // Nov. 1, 2012  // format: (year,month,day) where month=0..11 for jan..dec
    datearray.push(new Date(2012,10,15));
    datearray.push(new Date(2012,10,29));
    datearray.push(new Date(2012,11,12));  // Dec. 12, 2012
    datearray.push(new Date(2013,01,22));
    datearray.push(new Date(2013,03,20));
    // datearray.sort();
    datearray.sort(function(a,b){return a-b});
    
    var today = new Date();
    var nextLater = null;
    for (var i = 0; i < datearray.length; i++) {
      if (datearray[i] > today) { nextLater = datearray[i].toDateString();	break; }
    }
    
    // document.write(nextLater);
    // alert(nextLater.toDateString());
    window.onload = function() {
      document.getElementById('dateInfo').innerHTML = nextLater;
    }
    </script>
    
    </head>
    <body>
    <div id='dateInfo' />
    </body>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •