eonasdan Bootstrap datetimepicker invalid date conversion

寵の児 提交于 2019-12-12 02:34:15

问题


I have a DateTimePicker Bootstrap that has a fixed format.

$('.datetimepicker1').datetimepicker({
    format: 'ddd, DD MMM YYYY'
});

Everything works fine but when I try to paste a date on it with a different format, it converts it to a wrong date.

The date I pasted is 01/02/2016 and the result I get from the datetimepicker is Tue, 01 Jan 0002 when I hit tab.

It converts it to the date format I set but the date is wrong. I tried converting it onchange but I know there should be a cleaner and better way to handle this.

Any ideas? Thanks!


回答1:


The datetimepicker has extraFormats option that lets you specify multiple formats. In your case, you can use the following code as example:

$('.datetimepicker1').datetimepicker({
    format: 'ddd, DD MMM YYYY',
    extraFormats: ['ddd, DD MMM YYYY', 'DD/MM/YYYY']
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<div class="form-group">
  <div class="input-group date datetimepicker1">
    <input type="text" class="form-control" />
    <span class="input-group-addon"><span class="glyphicon-calendar glyphicon"></span></span>
  </div>
</div>


来源:https://stackoverflow.com/questions/38320809/eonasdan-bootstrap-datetimepicker-invalid-date-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!