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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    dates in descending order

    Can anyone help me in writting javascript to display the list of dates in the descending order?

  • #2
    New Coder
    Join Date
    Mar 2007
    Location
    italy
    Posts
    94
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by mamatha View Post
    Can anyone help me in writting javascript to display the list of dates in the descending order?
    can you explain yourself better? are these dates in a form? Are they in a array?

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java scripts

    Quote Originally Posted by carlitos_way View Post
    can you explain yourself better? are these dates in a form? Are they in a array?

    I want to know how to write a javascript "to sort a set of date and time in the ascending order" that is i am having 5 to 6 date&time values from the external application in the format "yyyy-mm-dd hh:mm:ss" and i want to write a function to display them in the ascending order using jscript.
    The dates are in a form.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Sounds like a homework asignment.
    First you specifed descending, then ascending order. So which is it?

    Have a look at the JavaScript sort() method.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Code:
    var dates = [
    "2002-05-11 05:12:43",
    "1999-06-01 11:01:13",
    "2003-02-24 10:10:33",
    "2001-05-12 03:11:26"
    ]
    
    
    var dates2=[];
    for(var i=0, mx = dates.length; i<mx;i++){
    var p = dates[i].split(/\D+/g);
     dates2[i]= new Date(p[0],p[1],p[2],p[3],p[4],p[5]);
    }
    
    
    //ascending:
    alert(dates2.sort(function(a,b){return a-b}).join("\n"))
    
    //descending:
    alert(dates2.sort(function(a,b){return b-a}).join("\n"))
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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