Answers.Body: <p>I was able to achieve this by including this JS snippet in the extension instead, posting here incase it helps someone.</p>
  <pre><code>var now = new Date();
  var day = (&quot;0&quot; + now.getDate()).slice(-2);
  var month = (&quot;0&quot; + (now.getMonth() + 1)).slice(-2);
  var today = now.getFullYear()+&quot;-&quot;+(month)+&quot;-&quot;+(day) ;
  $('#custom_315').val(today);
  </code></pre>
  <p>EDIT: It also works with following build form function, Thanks to Pradeep for the hints.</p>
  <pre><code>    function club_civicrm_buildForm($formName, &amp;$form) {
          if ($formName == 'CRM_Contribute_Form_Contribution_Main'
              &amp;&amp; in_array($form-&gt;getVar('_id'), [42])
            ) {
             //Populate Date using the function
              $defaultDate = date(&quot;Ymd&quot;);
              $defaults['custom_315'] = $defaultDate;
              $form-&gt;setDefaults($defaults);
             //alternatively addScriptFile function to inject js
              Civi::resources()-&gt;addScriptFile('club', 'club.js');
            }
          }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-29T14:23:15.000Z
Answers.Text: Do you agree to share your extension?
Answers.UserDisplayName: Alain Rihs
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-01T00:02:22.443Z
Answers.Text: You can add this to the PHP file of any custom extension. Find _buildform and add the function.
Answers.UserDisplayName: Tapash
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-03T07:57:34.330Z
Answers.Text: Oh OK, thank you, but I didn't develop a custom extension, so I have to see how I can do that to insert your code.
Answers.UserDisplayName: Alain Rihs
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-19T05:02:37.480Z
Answers.LastActivityDate: 2023-09-22T05:05:16.050Z
Answers.LastEditDate: 2023-09-22T05:05:16.050Z
Answers.UserDisplayName: Tapash
Body: <p>How to set default date today on a custom date field that is added on a contribution page via profile?</p>
  <p>I have tried to add this in my extension but doesnt seem to work and getting populated as 01/01/1970 set after form submitted
  .
  Any help would be appreciated. Thanks</p>
  <pre><code>function club_civicrm_buildForm($formName, &amp;$form) {
      if ($formName == 'CRM_Contribute_Form_Contribution_Main'
          &amp;&amp; in_array($form-&gt;getVar('_id'), [42])
        ) {
          $defaultDate = date(&quot;d/m/Y&quot;);
          $defaults['custom_315'] = $defaultDate;
          $form-&gt;setDefaults($defaults);
        }
      }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-09-19T14:41:29.030Z
Comments.Text: Its should be  $defaultDate = date("Ymd");
Comments.UserDisplayName: Pradeep Nayak
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-09-19T17:33:40.863Z
Comments.Text: !thanks @PradeepNayak-CircleInt. it worked with build form function as well now.
Comments.UserDisplayName: Tapash
Tags: <custom-fields><profile><extension><contribution-page>
Title: How to set TODAY as default date for a custom date field placed on a contribution page?
ThreadText: # How to set TODAY as default date for a custom date field placed on a contribution page?
  
  (Asked by: username_0 on 2023-09-19)
  
  How to set default date today on a custom date field that is added on a contribution page via profile?
  
  I have tried to add this in my extension but doesnt seem to work and getting populated as 01/01/1970 set after form submitted
  .
  Any help would be appreciated. Thanks
  
  ```
  function club_civicrm_buildForm($formName, &$form) {
      if ($formName == 'CRM_Contribute_Form_Contribution_Main'
          && in_array($form->getVar('_id'), [42])
        ) {
          $defaultDate = date("d/m/Y");
          $defaults['custom_315'] = $defaultDate;
          $form->setDefaults($defaults);
        }
      }
  
  ```
  
  **Tags:** custom-fields, profile, extension, contribution-page
  
  > **Comments:**
  > **username_1**: Its should be  $defaultDate = date("Ymd");  
  > **username_0**: !thanks @username_1-CircleInt. it worked with build form function as well now.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2023-09-19. Score: 2 | Accepted answer
  
  I was able to achieve this by including this JS snippet in the extension instead, posting here incase it helps someone.
  
  ```
  var now = new Date();
  var day = ("0" + now.getDate()).slice(-2);
  var month = ("0" + (now.getMonth() + 1)).slice(-2);
  var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
  $('#custom_315').val(today);
  
  ```
  
  EDIT: It also works with following build form function, Thanks to username_1 for the hints.
  
  ```
      function club_civicrm_buildForm($formName, &$form) {
          if ($formName == 'CRM_Contribute_Form_Contribution_Main'
              && in_array($form->getVar('_id'), [42])
            ) {
             //Populate Date using the function
              $defaultDate = date("Ymd");
              $defaults['custom_315'] = $defaultDate;
              $form->setDefaults($defaults);
             //alternatively addScriptFile function to inject js
              Civi::resources()->addScriptFile('club', 'club.js');
            }
          }
  
  ```
  
  > **Comments:**
  > **username_2**: Do you agree to share your extension?  
  > **username_0**: You can add this to the PHP file of any custom extension. Find _buildform and add the function.  
  > **username_2**: Oh OK, thank you, but I didn't develop a custom extension, so I have to see how I can do that to insert your code.  
  
  ---
  
  