Javascript help fixing a working time ago date function to simply show seconds ago.

Posted by Scarface on Stack Overflow See other posts from Stack Overflow or by Scarface
Published on 2010-03-25T23:23:40Z Indexed on 2010/03/25 23:33 UTC
Read the original article Hit count: 541

Filed under:

Hey guys quick question, I am using a javascript function and it works except I can only make it say 0 seconds ago, when the time is under a minute. Can anyone quickly explain what I am doing wrong?

function handleDate( timestamp ) {
        var n=new Date(), t, ago = " ";
        if( timestamp ) {
          t =   Math.round( (n.getTime()/1000 - timestamp)/60 );
          ago += handleSinceDateEndings( t, timestamp );
        } else {
            ago += "";
        }
        return ago;
    }    

    function handleSinceDateEndings( t, original_timestamp ) {
        var ago = " ", date;
        if( t <= 1 ) {
            ago += t + " seconds ago";
        } else if( t<60) {
            ago += t + " mins ago";
        } else if( t>= 60 && t<= 120) {
            ago += Math.floor( t / 60 ) + " hour ago"
        } else if( t<1440 ) {
            //console.log(t)
            ago += Math.floor( t / 60 )  + " hours ago";
        } else if( t< 2880) {
            ago +=  "1 day ago";
        } else if( t > 2880  && t < 4320 ) {
            ago +=  "2 days ago";
        } else {
            date = new Date( parseInt( original_timestamp )*1000 ) 
            ago += months[ date.getMonth() ] + " " + date.getDate();
        }


        return ago;
    }

    var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

© Stack Overflow or respective owner

Related posts about jQuery