Skip to content

Instantly share code, notes, and snippets.

@BigBlueHat
Last active September 27, 2015 11:08
Show Gist options
  • Save BigBlueHat/1259891 to your computer and use it in GitHub Desktop.
Save BigBlueHat/1259891 to your computer and use it in GitHub Desktop.
dateToArray - handy for emitting Cloudant & Apache CouchDB MapReduce keys based on dates
// 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);
}
}
@BigBlueHat
Copy link
Author

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. 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment