This guide will explain how to extract date and time values into separate form fields from a date&time field using JavaScript.
- Please add a Date&Time, Date and Time fields on the form:
- Next, please drag&drop a JavaScript field and copy the below code into it:
// 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 });
});
- Please change the field IDs in the JavaScript as shown below:
The field IDs can be copied when editing the fields:
Now once you capture a Date&Time field, the Date and Time fields will be automatically populated:
For any questions, please contact us - [email protected] or the Formyoula in-app chat. 🙂