1

I need to populate dropdown/ select list with calender dates in DD-MM-YYYY format, I have to do this in JavaScript and script should automatically fill Dropdown with dates for next 3 months.

I tried to look for such script but could not find. I would appreciate any help. i am open to use any jQuery if it works.

I have to fill dropdown with date in the format mentioned i cant use popup Calenders etc..

Example:

<select class="ddDates" id="Dates" name="Dates">
    <option value="10-01-2012" selected>10-01-2012</option>
    <option value="11-01-2012">11-01-2012</option>
    <option value="12-01-2012">12-01-2012</option>
    <option value="13-01-2012">13-01-2012</option>
</select>

I have searched google and i cant even find the logic how i can read system calender and populate the dropdown/ select list

1
  • i think you need to Google a little to find some logic for generating calender.. then you try displaying it as you want. try out some working stuff then will help you... Commented Jan 10, 2012 at 9:01

2 Answers 2

3

This code generates the markup in question using JavaScript (and jQuery)

function pad(n){return n<10 ? '0'+n : n}
var date = new Date();
var selectElement = $('<select>'), optionElement;
for (var count =0; count < 90; count++){
    date.setDate(date.getDate() + 1);
    formattedDate = pad(date.getUTCDate()) + '-' + pad(date.getUTCMonth()+1) + '-' +  date.getUTCFullYear();
    optionElement = $('<option>')
    optionElement.attr('value',formattedDate);
    optionElement.text(formattedDate);
    selectElement.append(optionElement);
}

You can change the value of count according to your logic.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried to call the function but it does not show anything <script type="text/javascript"> pad(2) </script>. I am doing something wrong
See it working here: jsfiddle.net/saQTw (pad just adds leading zero to single digit numbers, check the code :) )
2

jQuery UI's Datepicker: http://jqueryui.com/demos/datepicker/

6 Comments

I was thinking the same, but i cant use popup Calenders etc in the question means this may not be suitable for the OP.
That is not a popup, that is a Dropdown, just like OP asked.
I took dropdown to mean filling a native HTML select box - hence my first comment. Without clarification from the OP it's hard to tell exactly what he wants. This is the best solution though IMO.
Ok, I see what you mean. Perhaps some input from OP would clear this up.
@RoryMcCrossan I took the same meaning, hence is the solution I provided. Though, I also don't think giving dates of 3 months in a select box can be nice idea anywhere.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.