Setting a Form Field to the Current Date and Time

Version 2

    With these instructions, you can populate a hidden form field with the current date and time.

    Note: if you want a user-selectable date field, see this solution for using a calendar date picker in your form: Adding a Date Picker to a Form

    First, you need to add a hidden field to your form.  See these instructions if you don't know how: Making a Field Hidden on a Form

     

    Next, add the following Javascript to your landing page in a Custom HTML element. You'll need to update the field ID, highlighted in yellow, to match the ID of the hidden field you want to populate. These directions show you how to find that ID: Setting or getting a form field value via Javascript on a landing page

     

    Note: Please ensure that you have access to an experienced JavaScript developer.

    Marketo Technical Support is not set up to assist with troubleshooting JavaScript.

     

    To change the date format, change the dateString field below.

    <script language="Javascript" src="/js/public/jquery-latest.min.js" type="text/javascript"></script>

     

    <script type="text/javascript">
        // set no conflict mode for jquery
      var $jQ = jQuery.noConflict();

     

        // when the page is ready, add date
      $jQ(document).ready(function() {
        var myDate = new Date();
        var month = myDate.getMonth() + 1;
        var dateString = month + '/' + myDate.getDate() + '/' + myDate.getFullYear() + ','+ myDate.getHours() + ':' + myDate.getMinutes();
     
        $jQ('#fieldName').attr('value', dateString);
      });
    </script>