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!
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/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With