loverajoel / jstips

This is about useful JS tips!
http://jstips.co
GNU General Public License v3.0
12.5k stars 803 forks source link

Tip #49 easier unix timestamp #339

Closed ghost closed 8 years ago

ghost commented 8 years ago

Maybe an easier and faster way?

timestamp = (+new Date / 1000) | 0

Does anyone know if this would have any problems?

FagnerMartinsBrack commented 8 years ago

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.

lnfnunes commented 8 years ago

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.

zenopopovici commented 8 years ago

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