JavaScript time
Return current UNIX timestamp
1 2 3 4 56 7 8 9 1011 12 13 14 | function time () { // Return current UNIX timestamp // // version: 910.813 // discuss at: http://phpjs.org/functions/time // + original by: GeekFG (http://geekfg.blogspot.com) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: metjay // + improved by: HKM // * example 1: timeStamp = time(); // * results 1: timeStamp > 1000000000 && timeStamp < 2000000000 return Math.floor(new Date().getTime()/1000); } |
Examples
Running
1 | timeStamp = time(); |
Should result in
1 | timeStamp > 1000000000 && timeStamp < 2000000000 |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have time goodness in JavaScript.
@ HKM: Thanks for supplying the fix! Will be online shortly. Until then review at:
http://github.com/kvz/phpjs/commit/459b16b6d454d4e30386729ee2640d0013449578
It have 1 second error because the milliseconds will be counded. Therefore, the code should be changed to that.
1 2 3 | function time () { return Math.floor(new Date().getTime()/1000); } |


Kevin van Zonneveld
29 Nov '09