Reputation: 2070
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!
Upvotes: 0
Views: 1536
Reputation: 31482
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>
Upvotes: 3