function createDateTimePicker(datePicker, checkbox, date){ $(datePicker).datetimepicker({ //useCurrent: false, //Important! See issue #1075 locale: 'en-gb', format: 'DD-MM-YYYY HH:mm', keepInvalid: true, sideBySide: true, defaultDate: null, timeZone: moment.tz.guess() }); $(checkbox).change(function() { updateDateTimePicker(datePicker, checkbox) }); if (!$(checkbox)[0].checked){ if ($(datePicker)[0].value == ''){ $(datePicker)[0].value = date.format('DD-MM-YYYY HH:mm'); } else if (!$('.alert')[0]) { //Don't convert datetime again after error $(datePicker)[0].value = moment.utc($(datePicker)[0].value, 'DD-MM-YYYY HH:mm').local().format('DD-MM-YYYY HH:mm'); } } else { $(datePicker).data("DateTimePicker").disable(); }; } function updateDateTimePicker(picker, checkbox){ if ($(checkbox).prop("checked")) { $(picker).data("DateTimePicker").disable(); } else { $(picker).data("DateTimePicker").enable(); if ($(picker)[0].value == ''){ $(picker)[0].value = moment().format('DD-MM-YYYY HH:mm'); } } }