Skip to content

Instantly share code, notes, and snippets.

@eliperelman
Forked from 140bytes/LICENSE.txt
Created June 20, 2011 16:25
Show Gist options
  • Save eliperelman/1035932 to your computer and use it in GitHub Desktop.
Save eliperelman/1035932 to your computer and use it in GitHub Desktop.
Date.now polyfill for 140byt.es

Date.now polyfill for 140byt.es

This function will provided the ECMAScript 5 method of Date.now to browsers that do not support ECMAScript 5.

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.
})
Date.now||(Date.now=function(){return+new Date})
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.
{
"name": "DateNowPolyfillFor140bytes",
"description": "ECMAScript 5 polyfill for Date.now method",
"keywords": [
"Date",
"now",
"polyfill",
"JavaScript",
"ECMAScript5"
]
}
<!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>
@jed
Copy link

jed commented Jun 20, 2011

you don't need that trailing semicolon. it can only serve to anger @isaacs.

@eliperelman
Copy link
Author

Ah good catch, I was letting my best practices get to me ;)

@ErnWong
Copy link

ErnWong commented Dec 8, 2012

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)

@yckart
Copy link

yckart commented Feb 27, 2013

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