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: 1008.1718 // 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.
Just to let you know the getTime function is pretty innacurate in Fresco (a browser used in some devices such as TV set top boxes).
For example, the date 2011/03/03 22:59:00, when converted in Firefox is correctly translated to 1301864340, but in Fresco, it gets translated to 1301871540, which is really 2011/04/03 17:59:00 (a month off!).
So maybe, for accuracy's sake in all browsers, the date to timestamp conversion should be done manually instead of relying in getTime()
@ 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.
function time () {
return Math.floor(new Date().getTime()/1000);
}


OMA
Jul 14th