Question T12049
Visible to All Users

Specify the default DatePicker date

created 2 years ago (modified 2 years ago)

[Ticket cloned from T12036: Theme colors - How To?]

I have a date in the form of:

JavaScript
var date = new Date(model.PatientBirthDate); var localDateString = date.toLocaleDateString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit' });

So I would get something like "mm/dd/yyyy".

I have a datepicker in the survey, who asks for this specific date format, to which I am assigning the date value like this:

JavaScript
patientSurvey.getQuestionByName("birthDate").value = localDateString;

Where birthDate is the name of the question in the survey.

But the date won't get assigned to the datepicker.

Any tips would help.

Answers approved by surveyjs Support

created 2 years ago

Hi Catalin,
As outlined in jQuery DatePicker documentation (#method-setDate), the initial date may be any of the following:

The new date may be a Date object or a string in the current date format (e.g., "01/26/2009"), a number of days from today (e.g., +7) or a string of values and periods ("y" for years, "m" for months, "w" for weeks, "d" for days, e.g., "+1m +7d"), or null to clear the selected date.

So, I believe you can use the initial date value, without converting it to a local date string.

JavaScript
var date = new Date(model.PatientBirthDate); patientSurvey.getQuestionByName('birthDate').value = date;

Please let me know it this helps.
For the record: I created this example to ensure that assigning a JavaScript Date object works.

I look forward to your reply.

    Show previous comments (1)

      Hello Catalin,
      Sure, here is the example.

      JavaScript
      const birthDateFromModel = new Date(197164); function convertDateToString(date{     const toStr = (val) => {         if (val < 10return '0' + val.toString();         return val.toString();     };     return (         date.getFullYear() +         '-' +         toStr(date.getMonth() + 1) +         '-' +         toStr(date.getDate())     ); } survey.getQuestionByName( "birthDate").value = convertDateToString(birthDateFromModel);

      Thank you,
      Andrew
      SurveyJS Team

        Hello Andrew,

        Wonderful, all good now.

        Thanks a bunch!

        Catalin

          You are very welcome!

          Andrew
          SurveyJS Team