0

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!

4

1 回答 1

3

datetimepicker 具有extraFormats允许您指定多种格式的选项。在您的情况下,您可以使用以下代码作为示例:

$('.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>

于 2016-07-12T10:36:58.533 回答