Internet Explorer 11 以错误的格式向 Laravel 后端发送日期

Internet Explorer 11 sending date in incorrect format to Laravel backend

我有一个包含以下输入的表单:

<div class="form-group col-md-6 ">
    <label for="startdate">Start date</label>
    <input class="form-control" name="startdate" type="date" id="startdate">
</div>

我知道后端获取日期值的预期格式是 yyyy-mm-dd(HTML5 date input specification refers to the RFC3339 specification,它指定的完整日期格式等于:yyyy-mm-dd)。

然而,Internet Explorer 11 似乎以不同的格式发送值,因为我在 Laravel 日志中收到错误消息和以下消息(使用 Laravel 5.8):

[2019-12-03 20:41:16] production.ERROR: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '06/12/2019' for column 'startdate' at row 1 (SQL: insert into `absenses` (`motive`, `startdate`
at /var/www/zitthy/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, Doctrine\DBAL\Driver\PDOException(code: 22007): SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '06/12/2019' for column 'startdate'
at row 1 at /var/www/zitthy/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:123, PDOException(code: 22007): SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '06/12/2019' for column 'startdate'
at row 1 at /var/www/zitthy/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:121)

这在 Chrome 和 Firefox 中正常工作。

关于如何在 IE11 中修复此问题的任何提示?

谢谢

<input type="date"> 是 IE 11 的 not supported。它是 IE 中的简单 <input>。值的格式取决于您在 input 中键入的格式。那么如何处理IE中的值呢?

如果您不想手动处理该值,可以使用 polyfill 使 <input type="date"> 与 IE 兼容。您可以查看我下面的示例,使用 polyfill 后,IE 中输入的日期值与 Chrome 中的格式相同:

webshims.setOptions('forms-ext', {
  types: 'date'
});
webshims.polyfill('forms forms-ext');

function test() {
  var dateval = document.getElementById("startdate").value;
  console.log(dateval);
}
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://cdn.jsdelivr.net/webshim/1.14.5/polyfiller.js"></script>
<div class="form-group col-md-6 ">
  <label for="startdate">Start date</label>
  <input class="form-control" name="startdate" type="date" id="startdate">
  <input type="button" value="test" onclick="test()" />
</div>