Skip to content

Instantly share code, notes, and snippets.

@bbrown
Created December 1, 2014 17:38
Show Gist options
  • Save bbrown/fe3a17fa63a78b5e8b36 to your computer and use it in GitHub Desktop.
Save bbrown/fe3a17fa63a78b5e8b36 to your computer and use it in GitHub Desktop.
Formatting function for dates in JS (for those times when Moment.js isn't feasible)
function yyyymmddhhmm(date)
{
// Modified from http://stackoverflow.com/a/3067896/20595
var yyyy = date.getFullYear().toString();
var mm = (date.getMonth()+1).toString(); // getMonth() is zero-based
var dd = date.getDate().toString();
var hh = date.getHours().toString();
var min = date.getMinutes().toString();
return yyyy + (mm[1]?mm:"0"+mm[0]) + (dd[1]?dd:"0"+dd[0]) + (hh[1]?hh:"0"+hh[0]) + (min[1]?min:"0"+min[0]); // padding
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment