Skip to content

Instantly share code, notes, and snippets.

@alanthai
Last active August 29, 2015 14:07
Show Gist options
  • Save alanthai/f1e3587df1f935a954b2 to your computer and use it in GitHub Desktop.
Save alanthai/f1e3587df1f935a954b2 to your computer and use it in GitHub Desktop.
Useful date functions and statements
// midnight today
var today = new Date(new Date().setHours(0,0,0,0));
// midnight tomorrow
var tomorrow = new Date(new Date().setHours(24,0,0,0));
// date format: yyyy-mm-dd HH:MM:SS
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
formatDate = function(d) {
var yyyy = d.getFullYear(),
MM = pad(d.getMonth()+1, 2),
dd = pad(d.getDate(), 2),
HH = pad(d.getHours(), 2),
mm = pad(d.getMinutes(), 2),
ss = pad(d.getSeconds(), 2);
return [yyyy, MM, dd].join("-") + " " + [HH, mm, ss].join(":");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment