0

This is my code:

$("#text").each(function randomDate(start, end) {{
    var date = new Date(+start + Math.random() * (end - start));
    return date;
}

    var date1 = new Date(2000,09,01);
    var date2 = new Date();
    //alert($.datepicker.formatDate("mm/dd/yy",randomDate(date1, date2)));
});

I want to be able to randomly select a date and put it in a textbox using javascript or jquery. The $("text") is the id of the text box.

Basically I want a simple solution to select a random date that automatically gets entered in the date textbox. Any help is much appreciated. Thanks.

Anan Srivastava
  • 112
  • 1
  • 9
  • 4
    Please copy+paste your actual code in the question so that we can work with it. An image actually makes it harder for people to help you – Rory McCrossan Jan 10 '17 at 09:30
  • Also note that to select an element by its `id` you need a `#` in the selector, eg `$('#test')` – Rory McCrossan Jan 10 '17 at 09:34
  • 1
    @RoryMcCrossan: Thank you for your suggestion. I have edited my post. – Anan Srivastava Jan 10 '17 at 09:36
  • 2
    Possible duplicate of [Elegant method to generate array of random dates within two dates](http://stackoverflow.com/questions/9035627/elegant-method-to-generate-array-of-random-dates-within-two-dates) – Rahul Jan 10 '17 at 09:36

2 Answers2

1

This will help you out. check it

function randomDate(start, end) {
  var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "June",
  "July", "Aug", "Sept", "Oct", "Nov", "Dec"
    ];
  var date = new Date(new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())));
  var date = new Date(date);
  return date.getDate() + '-' + monthNames[(date.getMonth() + 1)] + '-' +  date.getFullYear();
}

$("#text").val(randomDate(new Date(2004, 0, 9), new Date()))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id = "text"/>
Bharat
  • 2,441
  • 3
  • 24
  • 36
  • Worked like a charm. Thank you. How do I get it in dd-month_name-yyyy format though? Example: 02-Jan-2017 – Anan Srivastava Jan 10 '17 at 10:00
  • I have slightly updated your code: var datewithformate = DateAndTimeFormate (randomDate(new Date(0000, 00, 00), new Date())); $("#text").val(datewithformate) ; var datewithformate1 = DateAndTimeFormate (randomDate(new Date(0000, 00, 00), new Date())); $("#text2").val(datewithformate1) I want (datewithformat1) to always be >= (datewithformat). The do while loop isin't helping. How can I achieve the above mentioned condition? – Anan Srivastava Jan 10 '17 at 12:42
0

The answer from Bharat Patidar will not sync with the jquery datepicker. You can dynamically update the datepicker with random date like below.

Create a separate function

function randomDate(start, end) {
    return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}

Update the datepicker object

$( "#text" ).datepicker( "setDate", randomDate(new Date(2004, 0, 9), new Date()));
Avinash
  • 4,115
  • 2
  • 22
  • 41