Convert Unix time to minutes ago in JavaScript

I want a hidden time () that PHP created and stored in our database a few minutes ago when it was captured by our JavaScript after a JSON request.

http://media.queerdio.com/mobileDevice/?uri=nowplaying/1

as you can see, we store time, for example

"airtime":1382526339

what we want to turn into

3 minutes ago

This is what we know.

First we need to run something like this

function convert_time(ts) {
   return new Date(ts * 1000) 
}

Taking airtime and running it through this function makes this compatible with JavaScript from what I read (I could be wrong) Unix time javascript

Bt, , JavaScript . jQuery, , , , , .

+4
4

- , javascript. unix "X time ago", - :

function time2TimeAgo(ts) {
    // This function computes the delta between the
    // provided timestamp and the current time, then test
    // the delta for predefined ranges.

    var d=new Date();  // Gets the current time
    var nowTs = Math.floor(d.getTime()/1000); // getTime() returns milliseconds, and we need seconds, hence the Math.floor and division by 1000
    var seconds = nowTs-ts;

    // more that two days
    if (seconds > 2*24*3600) {
       return "a few days ago";
    }
    // a day
    if (seconds > 24*3600) {
       return "yesterday";
    }

    if (seconds > 3600) {
       return "a few hours ago";
    }
    if (seconds > 1800) {
       return "Half an hour ago";
    }
    if (seconds > 60) {
       return Math.floor(seconds/60) + " minutes ago";
    }
}

, / .

, , , - :)

+6

Moment.js (8,8 kb ) javascript- . .

+4

Datejs. Date.

0

:

Moment.js unix ( ),

moment.unix(unixtime).startOf('hour').fromNow()

, (java System.currentTimeMillis()),

moment(millis).startOf('hour').fromNow()

: http://momentjs.com/docs/#/parsing/unix-offset/

0

All Articles