Answers.Body: <p>You don't say your version, but I'm guessing 4.7 from the description. The trick is to use ISO date format. So this should work:</p>
  
  <pre><code>function civitest_civicrm_buildForm($formName, &amp;$form) {
    $defaults['register_date'] = date("2017-04-15");
    $form-&gt;setDefaults($defaults);
  }
  </code></pre>
  
  <p>FWIW, this is still an issue for setting defaults via URL query string parameters, as of 4.7.22; there's now an open ticket with PR here: <a href="https://issues.civicrm.org/jira/browse/CRM-20922" rel="nofollow noreferrer">https://issues.civicrm.org/jira/browse/CRM-20922</a></p>
  
  <p>From <a href="https://issues.civicrm.org/jira/browse/CRM-18349" rel="nofollow noreferrer">https://issues.civicrm.org/jira/browse/CRM-18349</a>:</p>
  
  <blockquote>
    <p>In 4.7 custom fields of type date do not use the jcalendar.tpl but
    have been upgraded to use the quickform binding of
    <a href="https://wiki.civicrm.org/confluence/display/CRMDOC/crmDatepicker" rel="nofollow noreferrer">https://wiki.civicrm.org/confluence/display/CRMDOC/crmDatepicker</a></p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-21T13:47:24.160Z
Answers.Text: Hooray!!! Success at last! Thank you @TwoMice.
Answers.UserDisplayName: Jamie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-19T22:53:11.700Z
Answers.LastActivityDate: 2017-07-21T18:58:01.723Z
Answers.LastEditDate: 2017-07-21T18:58:01.723Z
Answers.UserDisplayName: TwoMice
Answers.Body: <p>I think the date widget have its default value based on the format it is being set. </p>
  
  <p>Eg - For <code>mm/dd/yyyy</code> input format set in <code>Administer -&gt; Localization -&gt; Date Formats</code>.</p>
  
  <p>This works -</p>
  
  <pre><code>function civitest_civicrm_buildForm($formName, &amp;$form) {
    $defaults['register_date'] = date("m/d/Y");
    $form-&gt;setDefaults($defaults);
  }
  </code></pre>
  
  <p>This doesn't -</p>
  
  <pre><code>$defaults['register_date'] = date("m-d-Y");
  $defaults['register_date'] = date("d/m/Y");
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-28T20:41:27.543Z
Answers.Text: Thanks for the answer. Unfortunately, it still doesn't seem to work (it appears I was already adding the right format). I just updated the question to show the output that I'm getting.
Answers.UserDisplayName: Jamie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-24T05:46:37.777Z
Answers.LastActivityDate: 2016-11-24T12:45:41.053Z
Answers.LastEditDate: 2016-11-24T12:45:41.053Z
Answers.UserDisplayName: jitendra - fuzion
Answers.Body: <p>Try inspecting and changing the form element type to a text element and then hide it via css.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-29T18:53:03.270Z
Answers.LastActivityDate: 2016-11-29T18:53:03.270Z
Answers.UserDisplayName: Alan Dixon
Body: <p>I have a custom module that invokes the civicrm_buildForm hook to set the default date for a given field to today's date.</p>
  
  <p>It is working - in part. When I examine the code I see that the default value is set. However, it is set in the input that is hidden. Whereas there is a second input that is displayed using what appears to be a randomly generated id (dp1479931180679) which seems to be created by the date picker widget.</p>
  
  <p>If you leave the field blank, the default date is picked up and works. But it's a usability issue since it doesn't appear to be filled out to the user. </p>
  
  <p>Anyone have a work around?</p>
  
  <p>Here is what the html code looks like:</p>
  
  <pre><code>&lt;span class="crm-form-date-wrapper"&gt;
  &lt;input id="custom_17" class="crm-form-text required crm-hidden-date" data-crm-custom="Constituent_Info__Individuals:Date_Started" data-crm-datepicker="{"date":"mm\/dd\/yy","minDate":"1986-01-01","maxDate":null,"time":false}" name="custom_17" value="11/28/2016" style="display: none;" type="text"&gt;
  &lt;input id="dp1480365364891" class="crm-form-text required crm-form-date hasDatepicker" style="" type="text"&gt;
  &lt;/span&gt;
  </code></pre>
  
  <p>The first input (which properly shows the right value) is hidden. The second one, which is the one displayed, doesn't have a value set.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-11-28T21:14:17.370Z
Comments.Text: Hm, your date format is "mm\/dd\/yy", and your date is "11/28/2016". Maybe it has to do with the escape character before the slash.
Comments.UserDisplayName: Kainuk - CiviCooP
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-30T08:50:03.637Z
Comments.Text: Where you able to figure this out?
Comments.UserDisplayName: Moe
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-01T14:08:24.933Z
Comments.Text: Yes - See @TwoMice solved my problem - however, I think the solution requires both answers.
Comments.UserDisplayName: Jamie
Tags: <hooks><dates>
Title: Setting default date value via civicrm_buildForm hook no longer displays date
ThreadText: # Setting default date value via civicrm_buildForm hook no longer displays date
  
  (Asked by: username_0 on 2016-11-23)
  
  I have a custom module that invokes the civicrm_buildForm hook to set the default date for a given field to today's date.
  It is working - in part. When I examine the code I see that the default value is set. However, it is set in the input that is hidden. Whereas there is a second input that is displayed using what appears to be a randomly generated id (dp1479931180679) which seems to be created by the date picker widget.
  If you leave the field blank, the default date is picked up and works. But it's a usability issue since it doesn't appear to be filled out to the user. 
  Anyone have a work around?
  Here is what the html code looks like:
  
  
  
  
  
  The first input (which properly shows the right value) is hidden. The second one, which is the one displayed, doesn't have a value set.
  
  **Tags:** hooks, dates
  
  > **Comments:**
  > **username_1**: Hm, your date format is "mm\/dd\/yy", and your date is "11/28/2016". Maybe it has to do with the escape character before the slash.  
  > **username_2**: Where you able to figure this out?  
  > **username_0**: Yes - See @username_3 solved my problem - however, I think the solution requires both answers.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2017-07-19. Score: 2 | Accepted answer
  
  You don't say your version, but I'm guessing 4.7 from the description. The trick is to use ISO date format. So this should work:
  function civitest_civicrm_buildForm($formName, &$form) {
    $defaults['register_date'] = date("2017-04-15");
    $form->setDefaults($defaults);
  }
  
  FWIW, this is still an issue for setting defaults via URL query string parameters, as of 4.7.22; there's now an open ticket with PR here: https://issues.civicrm.org/jira/browse/CRM-20922
  From https://issues.civicrm.org/jira/browse/CRM-18349:
  
  In 4.7 custom fields of type date do not use the jcalendar.tpl but
    have been upgraded to use the quickform binding of
    https://wiki.civicrm.org/confluence/display/CRMDOC/crmDatepicker
  
  > **Comments:**
  > **username_0**: Hooray!!! Success at last! Thank you @username_3.  
  
  ---
  
  ### Answer by username_4 on 2016-11-24. Score: 4
  
  I think the date widget have its default value based on the format it is being set. 
  Eg - For mm/dd/yyyy input format set in Administer -> Localization -> Date Formats.
  This works -
  function civitest_civicrm_buildForm($formName, &$form) {
    $defaults['register_date'] = date("m/d/Y");
    $form->setDefaults($defaults);
  }
  
  This doesn't -
  $defaults['register_date'] = date("m-d-Y");
  $defaults['register_date'] = date("d/m/Y");
  
  > **Comments:**
  > **username_0**: Thanks for the answer. Unfortunately, it still doesn't seem to work (it appears I was already adding the right format). I just updated the question to show the output that I'm getting.  
  
  ---
  
  ### Answer by username_5 on 2016-11-29. Score: 0
  
  Try inspecting and changing the form element type to a text element and then hide it via css.
  
  
  ---
  
  