Bootstrap datepicker — How to get toDisplay in other format and toValue in other format too

前端 未结 2 878
醉梦人生
醉梦人生 2021-01-14 17:54

For a long time, I\'m having trouble with toValue and toDisplay. I need and appear on the display to show the date in format dd.mm.yyyy an

相关标签:
2条回答
  • 2021-01-14 18:25
     $("#elementID").datepicker({
            format: "dd/mm/yyyy", // or what ever format your want
            calendarWeeks: true,
            todayHighlight: true,
            clearBtn: true,
            autoclose: true
        });
    

    Include moment.js library in your page then

    var date = moment($('#elementID').datepicker("getDate")).format("YYYY/MM/DD");
    
    0 讨论(0)
  • 2021-01-14 18:27

    I had exactly the same problem and struggled long to find a viable solution for this...

    In the end I had to give up on trying to getting the format right with that function, I simply used another hidden inputfield to store the dateformat to submit, which is updated on calendar change. I used moment.js to

    markup

     <input placeholder="dd.mm.yyyy" class="form-control datepicker" id="birthday" name="person[birthday]" value="01.01.1970" type="text"> 
     <input id="birthday-val" name="person[birthday]" value="1970-01-01" type="hidden"> 
    
    <script>
    $('.datepicker').datepicker({
        autoclose: true,
        locale:  'de',
        format:  'dd.mm.yyyy'
    });
    $('.datepicker').on('changeDate', function(e){
        //console.log( moment(e.date).format('YYYY-MM-DD') );
        $(this).next('input[type=hidden]').val( moment(e.date).format('YYYY-MM-DD') );
    });
    </script>
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题