1

How would I code this:

var randomDate = some random date between 01/01/2015 and 12/31/2015 formatted in mm/dd/yyyy;

alert(randomDate);

Barmar
  • 741,623
  • 53
  • 500
  • 612
Kirk Ross
  • 6,413
  • 13
  • 61
  • 104

1 Answers1

4

Try this you need jquery UI as well with this solution:

function randomDate(start, end) {
  var date = new Date(+start + Math.random() * (end - start));
  return date;
}
var date1 = new Date(2013,09,01);
var date2 = new Date(2013,09,20);
alert($.datepicker.formatDate("mm/dd/yy",randomDate(date1, date2)));

Updated JS fiddle: http://jsfiddle.net/f5q91cxn/2/

Aaron
  • 1,361
  • 1
  • 13
  • 30
  • this works but returns the loooooong date: Sat Jul 19 2014 23:53:35 GMT-0700 (PDT)" — how can I format it to mm/dd/yyyy? – Kirk Ross Oct 28 '15 at 20:02
  • @KirkRoss, please read about methods associated with the javascript Date object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – Jonathan M Oct 28 '15 at 20:48
  • @KirkRoss take a look: [updated version of this function](http://jsfiddle.net/t94778xt/) – Neodan Oct 28 '15 at 20:53
  • See the update i just added. This should do it for you but you need jquery UI as well. – Aaron Oct 29 '15 at 19:05