最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

time - Milliseconds passed since last minute in javascript - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question edited Oct 25, 2012 at 18:26 Michael Berkowski 271k47 gold badges450 silver badges394 bronze badges asked Oct 25, 2012 at 18:25 jtrickjtrick 1,36918 silver badges23 bronze badges 4
  • (Date.getSeconds()*1000) + Date.getMilliseconds() is the most reasonable way. – Denys Séguret Commented Oct 25, 2012 at 18:26
  • There's a lot about the JavaScript Date implementation that is inelegant (partly modeled after Java's implementation). – Michael Berkowski Commented Oct 25, 2012 at 18:27
  • 1 If you have two dates, just subtracting one from the other gives you the difference in milliseconds. It's a little unclear what you're trying to do. – Heretic Monkey Commented Oct 25, 2012 at 18:28
  • I'm just going for greater precision than seconds for an animation that I'm running. It I set it to cycle once per minute, so I just needed the milliseconds passed during the current clock minute. I used the 'awkward' method in my original post and as remended by dystroy above. Thanks for the ments. – jtrick Commented Oct 26, 2012 at 17:16
Add a ment  | 

2 Answers 2

Reset to default 6

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/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./)

What about…

Date.now() % 60000

Date.now returns the current UNIX timestamp in ms.


To clarify what's happening there, the % operator is called modulo and what it does is that it gives you the remainder from dividing the first number by the other one.

An example can be:

20 % 7 === 6
13 % 7 === 6
 6 % 7 === 6

…because…

20 / 7 = 2 + 6 / 7
13 / 7 = 1 + 6 / 7
 6 / 7 = 0 + 6 / 7

(note the remainder 6)

发布评论

评论列表(0)

  1. 暂无评论