3

I'm making an edit profile form, and I'm using jquery iu datepicker to select DOB, now when user edits its data he/she has already some date in the profile field, now if the user wanted to change the date the datepicker pops up, how can I make the date from profile field to be selected on the date picker,

or simply how can I make datepicker display specific date when opened here is my current script:

$('#member_dob').datepicker({
            changeMonth: true,
            changeYear: true,
            defaultDate: ,
            yearRange: '1970:1992',
            dateFormat: 'yy-mm-dd',
            monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'],
            dayNamesMin: ['Ned', 'Pon', 'Uto', 'Sri', 'Čet', 'Pet', 'Sub']
        });

So for instance if my dob is 20.05.1987 , how could I set datepicker to display that date when opened, or just month and a year

5 Answers 5

7

You can assign a date object to default date setting to datepicker widget, for instance as default date 1980, February 2nd:

var d = new Date(1980, 2, 2);
$('#member_dob').datepicker({
            changeMonth: true,
            changeYear: true,
            defaultDate: d,
            yearRange: '1970:1992',
            dateFormat: 'yy-mm-dd',
            monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'],
            dayNamesMin: ['Ned', 'Pon', 'Uto', 'Sri', 'Čet', 'Pet', 'Sub']
        });

This should do what you need,

hope it helps, Sinan.

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

Comments

4

If you want to change an existing datepicker value you can do this :

$('#txtDate').datepicker().datepicker('setDate', 'today');

1 Comment

If the date picker already exist then $('#txtDate').datepicker('setDate', 'today'); is enough
3

You can put the default date into the input field:

<input name="date" id="member_dob" value="87-05-20" />

DatePicker will automatically selected that date when opened. Make sure the value match with dateFormat that you're using.

Comments

1

Im not sure if I get right what you try to accomplish here. First of all you need to pass the current DOB from the serverside to Javascript.

You can do that in two ways. One is to build a jsonObject, for example:

var jsonDate = {date : '1987-05-20'};

And then in your datepicker:

$('#member_dob').datepicker({
            changeMonth: true,
            changeYear: true,
            defaultDate: jsonDate.date,
            yearRange: '1970:1992',
            dateFormat: 'yy-mm-dd',
            monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'],
            dayNamesMin: ['Ned', 'Pon', 'Uto', 'Sri', '&#268;et', 'Pet', 'Sub']
});

The second is to set a value for the Input you trigger you datePicker on :

$('#member_dob').datepicker({
            changeMonth: true,
            changeYear: true,
            yearRange: '1970:1992',
            dateFormat: 'yy-mm-dd',
            monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'],
            dayNamesMin: ['Ned', 'Pon', 'Uto', 'Sri', '&#268;et', 'Pet', 'Sub']
   });

<input type="text" id="member_dob" value="1987-05-20">

Comments

1

This works for me:

$(function() {
      $( "#datepicker" ).datepicker({
          changeMonth: true,
          changeYear: true,
          firstDay: 1,
          showAnim: 'slideDown',
          dateFormat: 'yy-mm-dd',
        });
      $("#datepicker").datepicker("setDate", new Date());
     });

Comments

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.