Extracting Date And Time From A Date&Time Into Separate Form Fields

This guide will explain how to extract date and time values into separate form fields from a date&time field using JavaScript.

  1. Please add a Date&Time, Date and Time fields on the form:
image
  1. Next, please drag&drop a JavaScript field and copy the below code into it:
image
// Attach listener for successful value set on the DateTime field
window.formyoula.form_fields['55d7-a210-79f9'].on('input:set:success', function (event) {
  // Get the updated value
  var datetime_field_value = window.formyoula.form_fields['55d7-a210-79f9'].get('value');
  // Use moment to parse
  var moment_object = moment(datetime_field_value);
  // Format date as yyyy-MM-dd (for input[type="date"])
  var formatted_date_string = moment_object.format('YYYY-MM-DD');
  // Format time as HH:mm (24-hour format for input[type="time"])
  var formatted_time_string = moment_object.format('HH:mm');
  // Set formatted values in target fields
  window.formyoula.form_fields['5653-bdaa-b9f7'].set({ value: formatted_date_string });
  // Set formatted values in target fields
  window.formyoula.form_fields['0ecd-ceaf-89ed'].set({ value: formatted_time_string });
});
  1. Please change the field IDs in the JavaScript as shown below:
image

The field IDs can be copied when editing the fields:

image

Now once you capture a Date&Time field, the Date and Time fields will be automatically populated:

image

For any questions, please contact us - [email protected] or the Formyoula in-app chat. 🙂