Skip to content

Instantly share code, notes, and snippets.

@theamanbhargava
Created May 28, 2017 11:35
Show Gist options
  • Save theamanbhargava/7bb708989b745e608b4f94c7049aa99c to your computer and use it in GitHub Desktop.
Save theamanbhargava/7bb708989b745e608b4f94c7049aa99c to your computer and use it in GitHub Desktop.
Random Date Generator
<!--Random Date Generator
Generates date in YYYY-MM-DD format
Courtesy : Different answers on SO
-->
//change value of multiplier of Math.random() to get older dates
var date = new Date(+(new Date()) - Math.floor(Math.random()*500000000000));
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
date = formatDate(date);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment