This function will provided the ECMAScript 5 method of Date.now to browsers that do not support ECMAScript 5.
-
-
Save eliperelman/1035932 to your computer and use it in GitHub Desktop.
Date.now polyfill for 140byt.es
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Date.now || // Use the native Date.now method if available, | |
(Date.now = function () { // Otherwise defined it here: | |
return +new Date // Create a new Date object and return the Unix timestamp epoch. | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Date.now||(Date.now=function(){return+new Date}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Eli Perelman http://eliperelman.com | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "DateNowPolyfillFor140bytes", | |
"description": "ECMAScript 5 polyfill for Date.now method", | |
"keywords": [ | |
"Date", | |
"now", | |
"polyfill", | |
"JavaScript", | |
"ECMAScript5" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Date.now polyfill for ECMAScript 5</title> | |
<script> | |
Date.now||(Date.now=function(){return+new Date}) | |
console.log(Date.now()); // logs current Unix time. | |
</script> |
Ah good catch, I was letting my best practices get to me ;)
Maybe I'm wrong, but according to the results of http://jsperf.com/date-gettime-vs-date-now/3 (new Date()).getTime()
is faster than +new Date
(although it uses more space)
Bigger, but better:
Date.now=Date.now||function(){return new Date().getTime()}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you don't need that trailing semicolon. it can only serve to anger @isaacs.