Question

I need to get the minutes passed from certain date(in rails server) to current date(in js).

Rails code:

return secTime = record.last_updated.to_i

Js code:

new Date(Number(secTime)*1000).toISOString()
=> "2014-02-09T12:21:31.000Z"

I would like to do like this:

new Date(Number(secTime)*1000).toISOString().getMinutes()

But it's not working...

Was it helpful?

Solution

(new Date() - new Date(Number(secTime)*1000)) / (60*1000)

You can just subtract Date objects like numbers.

OTHER TIPS

function minutesPassed(d) {
   var minutes = (new Date() - d) / (1000*60);
   return minutes;
}

var certainDate = (new Date().getTime()) - 1000*60*20; // i.e, 20 minutes ago
alert(minutesPassed(certainDate) + " minutes passed") // "20 minutes passed"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top