无法更改Bootstrap Datepicker格式

时间:2017-11-16 15:07:31

标签: bootstrap-datepicker

我想要一个这种格式的日期选择器:dd/mm/yyyy。当我在Javascript中设置默认日期时,我的日期格式正确。但是,当我单击它以选择另一个日期时,格式将重置为:mm/dd/yyyy

HTML code:

<div class="input-group" id="datepicker">
  <input type="text" class="form-control" data-bind="value: startDate, event: {change: savePerishableDate}" id="date"/>
</div>

Javascript datepicker:

<script type="text/javascript">
    $(document).ready(function() {
        $("#date").datepicker({
            format: "dd/mm/yyyy",
            language: "fr",
            changeMonth: true,
            changeYear: true
        });
    });
</script>

使用Javascript:

self.startDate(new Date)
tmpDate = new Date
self.startDate(tmpDate.toLocaleDateString("fr-FR"))
$('#date').datepicker 'setDate', self.startDate()

1 个答案:

答案 0 :(得分:2)

我认为您使用旧版本的bootstrap datepicker,使用此最新版本可以根据需要设置dateformat

$(document).ready(function() {
            $("#date").datepicker({
                format: "dd/mm/yyyy",
                language: "fr",
                changeMonth: true,
                changeYear: true
            });
        });
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<link type="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker3.min.css"/>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

<div class="input-group" id="datepicker">
  <input type="text" class="form-control" data-bind="value: startDate, event: {change: savePerishableDate}" id="date"/>
</div>