Skip to content

Instantly share code, notes, and snippets.

@uzbekdev1
Created April 8, 2019 16:23
Show Gist options
  • Save uzbekdev1/96c1bbae04e310ec6b4156a66f28b4a1 to your computer and use it in GitHub Desktop.
Save uzbekdev1/96c1bbae04e310ec6b4156a66f28b4a1 to your computer and use it in GitHub Desktop.
Date.prototype.toJSON
function(
a, // Placeholder or else we'll affect global
){
return // Splitting -here- gives us the full time and correct UTC values
(a=this.toUTCString().split(' '))[3] // Year
+ ((m=this.getMonth()+1)<9?'-0'+m:-m) // Month with padding (split value is month name)
+ '-'
+ a[1] // Day (pre-padded)
+ 'T' // Date-time delimiter
+ a[4] // Full time (pre-padded)
+ '.' // Milliseconds with padding
+ (this/1+'Z').slice(-3) // this/1 returns the number of millis
}
<!DOCTYPE html>
<title>Date.prototype.toJSON</title>
<style>body {font:12pt Courier}</style>
<script>
Date.prototype.JSON =
function(a){return(a=this.toUTCString().split(' '))[3]+('-0'+(this.getMonth()+1)).slice(-3)+'-'+a[1]+'T'+a[4]+'.'+(this/1+'Z').slice(-4)}
// } 140bytes
var d = new Date();var c = (navigator.userAgent.indexOf('Firefox')>-1)? 0 : 1;
</script>
<div>Current UTC time&nbsp;&nbsp;: <script>document.write(d.toUTCString());</script></div>
<div>140bytes version&nbsp;&nbsp;: <script>document.write(d.JSON());</script></div>
<div>Reference UTC time: <script>document.write(d.toJSON());</script></div>
<div>Current length: <script>document.write(Date.prototype.JSON.toString().replace(/\s/g,'').length+c);</script> bytes</div>
function(a){return(a=this.toUTCString().split(' '))[3]+('-0'+(this.getMonth()+1)).slice(-3)+'-'+a[1]+'T'+a[4]+'.'+(this/1+'Z').slice(-4)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Chris Hallberg <http://crhallberg.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": "DateToJSON",
"description": "Convert a Date object into a JSON string using UTC values.",
"keywords": [
"date",
"json",
"wishlist",
"crossbrowser"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment