-
-
Save BigBlueHat/1259891 to your computer and use it in GitHub Desktop.
// MIT Licensed - http://choosealicense.com/licenses/mit/ | |
function dateToArray(ts, length, utc) { | |
var length = length || 6, | |
d = (typeof ts === 'number' ? new Date(ts) : new Date()), | |
utc = Boolean(utc) || true; | |
if (utc) { | |
return [d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), | |
d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), | |
d.getUTCMilliseconds()] | |
.slice(0, length); | |
} else { | |
return [d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), | |
d.getMinutes(), d.getSeconds(), d.getMilliseconds()] | |
.slice(0, length); | |
} | |
} |
That's actually how I had it--as my typo on line 4 might prove. :) UTC is the default in any case. I'll fix the gist, and we can discuss internal variable naming following that. :)
ES5 has planned support for ISO8601, and it can be shimmed:
http://frugalcoder.us/post/2010/01/07/EcmaScript-5s-Date-Extensions.aspx
In my own code, I converted it to string with JSON.stringify()
and then split it up with a regex. But I think old versions of "couch" (actually Spidermonkey) don't provide the correct stringification. Also you get a string of a quoted string, so you have to chomp those out, so it's a wash at best I guess.
I typically use this now: http://momentjs.com/docs/#/displaying/as-array/
More to include...but then you've got all the moment-us power available. 😸
Hi, Benjamin. Since Couch apps can be both run by and accessed by computers all around the world, I would think UTC should be the default, not the option. Maybe call it
local
and switch the if and else parts?