loverajoel / jstips

This is about useful JS tips!

Home Page:http://jstips.co

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tip #49 easier unix timestamp

opened this issue · comments

Maybe an easier and faster way?

timestamp = (+new Date / 1000) | 0

Does anyone know if this would have any problems?

Is timestamp = (+new Date / 1000) | 0 intended to be the same as timestamp = ~~(+new Date / 1000) which is intended to be the same as Math.floor(+new Date / 1000)? If yes, then it is worth pointing out the benefits of each one so that one can decide the tradeoffs.

I've no cons about neither of the options, just the sintaxe verbosity...
In terms of performance, Date.now() looks faster than new Date in my tests.

Leaving as it is right now. If you have a suggestion, please make a PR with addendum to the tip.