###わかったこと
1、punchInの「2019-10-15 00:53:34」は取得後に分解できる
2、編集後のpunchInの日時は、元の形式(yyyy-mm-dd hh:mm:ss)で保存しなければエラーになる。 ※DBでpunchInはDatetime型で定義したので
###(自分なりの)解決方法
View
1
2<div class="form-group row">
3 <label class="col-md-3">出勤時間</label>
4 <input type="hidden" id="punchIn" name="punchIn"
5 value="{{ old('punchIn', $task->punchIn) }}">
6 <input type="text" id="punchInYmd">
7 <select id="selectPunchInHour"></select>時
8 <select id="selectPunchInMinute"></select>分
9</div>
10
jQuery
1$(function(){
2
3 //時分のプルダウン
4 var hh = [
5 '00',
6 '01',
7 '02',
8 '03',
9 '04',
10 '05',
11 '06',
12 '07',
13 '08',
14 '09',
15 '10',
16 '11',
17 '12',
18 '13',
19 '14',
20 '15',
21 '16',
22 '17',
23 '18',
24 '19',
25 '20',
26 '21',
27 '22',
28 '23',
29 ];
30
31 var mm = [
32 '00',
33 '01',
34 '02',
35 '03',
36 '04',
37 '05',
38 '06',
39 '07',
40 '08',
41 '09',
42 '10',
43 '11',
44 '12',
45 '13',
46 '14',
47 '15',
48 '16',
49 '17',
50 '18',
51 '19',
52 '20',
53 '21',
54 '22',
55 '23',
56 '24',
57 '25',
58 '26',
59 '27',
60 '28',
61 '29',
62 '30',
63 '31',
64 '32',
65 '33',
66 '34',
67 '35',
68 '36',
69 '37',
70 '38',
71 '39',
72 '40',
73 '41',
74 '42',
75 '43',
76 '44',
77 '45',
78 '46',
79 '47',
80 '48',
81 '49',
82 '50',
83 '51',
84 '52',
85 '53',
86 '54',
87 '55',
88 '56',
89 '57',
90 '58',
91 '59',
92 ];
93
94 // --------------------------
95 //「2019-10-15 00:53:34」をhhhh-mm-dd、hh、mm、ssに分解
96 var punchIn = $('#punchIn').val();
97 var punchInSplit = punchIn.split(' ');
98 var punchInYmd = punchInSplit[0];
99 var punchInHis = punchInSplit[1];
100 var punchInHisSplit = punchInHis.split(':');
101 var phnchInH = punchInHisSplit[0];
102 var phnchInM = punchInHisSplit[1];
103 var phnchInS = punchInHisSplit[2];
104
105 // --------------------------
106 //<select>タグのデフォルト値(hh)を設定
107 var hh,
108 $selectPunchInHour = $('#selectPunchInHour'),
109 $optionPunchInHours,
110 optionsPunchInHours,
111 isSelectedPunchInHour;
112 optionsPunchInHours = $.map(hh, function(name, value){
113 isSelectedPunchInHour = (name === phnchInH);
114 $optionPunchInHours = $('<option>', {value: value, text: name, selected: isSelectedPunchInHour});
115 return $optionPunchInHours;
116 });
117
118 $selectPunchInHour.append(optionsPunchInHours);
119
120 // --------------------------
121 //<select>タグのデフォルト値(mm)を設定
122 var mm,
123 $selectPunchInMinute = $('#selectPunchInMinute'),
124 $optionPunchInMinutes,
125 optionsPunchInMinutes,
126 isSelectedPunchInMinute;
127 optionsPunchInMinutes = $.map(mm, function(name, value){
128 isSelectedPunchInMinute = (name === phnchInM);
129 $optionPunchInMinutes = $('<option>', {value: value, text: name, selected: isSelectedPunchInMinute});
130 return $optionPunchInMinutes;
131 });
132
133 $selectPunchInMinute.append(optionsPunchInMinutes);
134
135 // --------------------------
136 //<input type="text" id="punchInYmd">にカレンダーを設定
137 $('#punchInYmd').val(punchInYmd);
138 $('#punchInYmd').datepicker({
139 dateFormat: 'yy-mm-dd',
140 });
141
142
143 // --------------------------
144 //id=editBtnのボタンクリックしたら編集した年月日時分秒を全て繋げて
145 //[2019-10-15 00:53:34]と同じ形式にする
146 $('#editBtn').click(function(){
147 var punchInYmdEdited = $('#punchInYmd').val();
148 var punchInHEdited = $('select[id=selectPunchInHour] > option:selected').text();
149 var punchInMEdited = $('select[id=selectPunchInMinute] > option:selected').text();
150 $('#punchIn').val(punchInYmdEdited + ' ' + punchInHEdited + ':' + punchInMEdited + ':' + phnchInS);
151 });
152
153
154
155});
156
jQueryに関しては絶対もっとキレイな書き方があるのだろうが、
今のレベルでは到達できない。一応動きとしては
●年月日部分はカレンダーが出現

●試しにpunchInを「2019-10-15 00:53:34」
↓
「2019-10-14 23:59:34」に変更しDBへ保存すると・・


無事punchInカラムの日時(datetime型)を変更することに成功しました。
・・が、もっとキレイなコード書けるように頑張らねばならない。
以上