Skip to content

Instantly share code, notes, and snippets.

@theamanbhargava
Created May 28, 2017 11:35
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