1
votes

I want to have a datepicker with this format: dd/mm/yyyy. When I set the default date in Javascript I have my date with the good format. But when I click it to choose an another date the format is reset at: 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
this is the full js? - user4332766
I edit my message, but it's just for the default date - Gajiu
sometimes you don't see changes because of Browser cache. Try it in a new private/incognito window in your browser. - Max Ivak

1 Answers

2
votes

I think you are using old version of bootstrap datepicker,Using this latest you can set dateformat as you want

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