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 3 of 3

Thread: Sort Date

  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sort Date

    This sort function does not seem to work with my date format which is
    1/01/2004 no leading 0 for month. Is there anything that could be
    changed so it would sort properly. It is part of a Table Sort script
    (sorttable.js) Thanks


    function ts_sort_date(a,b) {
    // y2k notes: two digit years less than 50 are treated as 20XX,
    greater than 50 are treated as 19XX
    aa = ts_getInnerText(a.cells[SORT_C*OLUMN_INDEX]);
    bb = ts_getInnerText(b.cells[SORT_C*OLUMN_INDEX]);
    if (aa.length == 10) {
    dt1 = aa.substr(6,4)+aa.substr(3,2)+*aa.substr(0,2);
    } else {
    yr = aa.substr(6,2);
    if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
    dt1 = yr+aa.substr(3,2)+aa.substr(0,*2);
    }
    if (bb.length == 10) {
    dt2 = bb.substr(6,4)+bb.substr(3,2)+*bb.substr(0,2);
    } else {
    yr = bb.substr(6,2);
    if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
    dt2 = yr+bb.substr(3,2)+bb.substr(0,*2);
    }
    if (dt1==dt2) return 0;
    if (dt1<dt2) return -1;
    return 1;

  • #2
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Convert them to dates

    Code:
    aDate = new Date(aa);
    Then test them. Right now you are doing a string test.
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #3
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply but I'm not sure what to do with the code. I'm not a javascript guy but really need to get this to work. Any help would be appreciated.


  •  

    Posting Permissions

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