Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Milliseconds passed since last minute in javascript

I'm probably just tired and not thinking clearly, but can someone give a concise way of getting the number of milliseconds passed since the last minute using javascript?

Something like Date.getSeconds(), but that would return milliseconds.

Although I could just do (Date.getSeconds()*1000) + Date.getMilliseconds(), this just seems really awkward and like there has to be a better way.

Thanks!

like image 629
jtrick Avatar asked Dec 06 '25 09:12

jtrick


1 Answers

Depends what you're trying to do.

The difference between now and 1 minute ago in milliseconds should always be 60000 milliseconds. o_O

As Jan said Date.now() will return the current timestamp in milliseconds.

But it seems you might be looking for the getTime method, e.g.: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getTime

// note the keyword "new" below
var date_instance = new Date();

// separate example in case you're managing Date()'s and not the method,
// who knows, just an example
var timestamp     = date_instance.getTime();

var minute_before_timestamp = function(ts){ 
  return ts - 60000;
};
console.log(minute_before_timestamp(timestamp));
console.log(minute_before_timestamp(date_instance.getTime()); // always same as above!

// or use the current time
console.log(minute_before_timestamp(Date.now()));
console.log(minute_before_timestamp(new Date().getTime()));

(another useful link: http://www.epochconverter.com/)

like image 116
notacouch Avatar answered Dec 08 '25 21:12

notacouch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!