Skip to content

Instantly share code, notes, and snippets.

@mdunham
Created March 18, 2018 11:33
Show Gist options
  • Save mdunham/e2315762ba1206fe19f2b34dc9382f80 to your computer and use it in GitHub Desktop.
Save mdunham/e2315762ba1206fe19f2b34dc9382f80 to your computer and use it in GitHub Desktop.
Convert cakephp 3 date time select inputs into html5 date and time inputs
/**
Takes this output from CakePHP:
<div class="row">
<div class="col-md-2">
<select name="scheduled_start[year]" class="form-control"><option ...></select>
</div>
<div class="col-md-2">
<select name="scheduled_start[month]" class="form-control"><option ...></select>
</div>
<div class="col-md-2">
<select name="scheduled_start[day]" class="form-control"><option ...></select>
</div>
<div class="col-md-2">
<select name="scheduled_start[hour]" class="form-control"><option ...></select>
</div>
<div class="col-md-2">
<select name="scheduled_start[minute]" class="form-control"><option ...></select>
</div>
</div>
And convert it to:
<div class="row">
<div class="col-md-2" ..HIDDEN..>
<div class="col-md-2" ..HIDDEN..>
<div class="col-md-2" ..HIDDEN..>
<div class="col-md-2" ..HIDDEN..>
<div class="col-md-2" ..HIDDEN..>
<div class="col-md-2" ..HIDDEN..>
<div class="col-md-6">
<input id="bds_scheduled_start" type="date">
</div>
<div class="col-md-6">
<input id="bds_scheduled_start" type="time">
</div>
</div>
To use just include the javascript below, but you may have to adjust the classes since I am using the bootstrap form helper.
*/
[].map.call(document.querySelectorAll('select[name$="[year]"]'), function(yearSel) {
var
realName = yearSel.getAttribute('name').replace('[year]', ''),
parent = yearSel.parentNode.parentNode,
daySel = parent.querySelector('select[name="' + realName + '[day]"]'),
monthSel = parent.querySelector('select[name="' + realName + '[month]"]'),
hourSel = parent.querySelector('select[name="' + realName + '[hour]"]'),
minSel = parent.querySelector('select[name="' + realName + '[minute]"]'),
newInput = document.createElement('input'),
newInput2 = document.createElement('input'),
dateVal = '';
if (monthSel && daySel && yearSel) {
if (monthSel.value && daySel.value && yearSel.value) {
dateVal = monthSel.value + '/'
+ daySel.value + '/'
+ yearSel.value;
}
newInput.setAttribute('id', 'bds_mdy' + realName);
newInput.setAttribute('type', 'date');
newInput.setAttribute('class', yearSel.getAttribute('class'));
newInput.setAttribute('value', dateVal);
parent.appendChild(newInput);
if (hourSel && minSel) {
newInput.outerHTML = '<div class="col-md-6">' + newInput.outerHTML + '</div>';
}
newInput.addEventListener('change', function(e) {
var
newDate = this.value,
dateSplit;
if (newDate.length === 10) {
newDate = new Date(newDate);
monthSel.value = ((newDate.getMonth() < 9) ? '0' : '') + (parseInt(newDate.getMonth()) + 1);
daySel.value = ((newDate.getDate() < 9) ? '0' : '') + (parseInt(newDate.getDate()) + 1);
yearSel.value = newDate.getFullYear();
}
});
daySel.parentNode.setAttribute('style', 'display: none!important');
monthSel.parentNode.setAttribute('style', 'display: none!important');
yearSel.parentNode.setAttribute('style', 'display: none!important');
}
if (minSel && hourSel) {
newInput2.setAttribute('id', 'bds_mh' + realName);
newInput2.setAttribute('type', 'time');
newInput2.setAttribute('class', hourSel.getAttribute('class'));
newInput2.setAttribute('value', hourSel.value + ':' + minSel.value);
parent.appendChild(newInput2);
if (daySel && yearSel && monthSel) {
newInput2.outerHTML = '<div class="col-md-6">' + newInput2.outerHTML + '</div>';
}
newInput2.addEventListener('change', function(e) {
var
newTime = this.value;
if (newTime.indexOf(':') !== -1) {
newTime = newTime.split(':');
hourSel.value = newTime[0];
minSel.value = newTime[1];
}
});
hourSel.parentNode.setAttribute('style', 'display: none!important');
minSel.parentNode.setAttribute('style', 'display: none!important');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment