set default time in bootstrap-datetimepicker

2019-01-14 16:30发布

I want to set default time in this datetimepicker as 00:01 for the current date. Anyone tried that before? Having a tough time with it. It Seems simple.

$('#startdatetime-from').datetimepicker({
    language: 'en',
    format: 'yyyy-MM-dd hh:mm'
});

I need to use setDate, but I'm not sure how. I checked their code, but did not find a parameter for that.

10条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-14 16:32

i tried to set default time for a picker and it worked:

$('#date2').datetimepicker({
 format: 'DD-MM-YYYY 12:00:00'
});

this will save in database as : 2015-03-01 12:00:00

used for smalldatetime type in sql server

查看更多
Melony?
3楼-- · 2019-01-14 16:32

This is my solution for your problem :

$('#startdatetime-from').datetimepicker({
language: 'en',
format: 'yyyy-MM-dd hh:mm'
}).on('dp.change', function (e) {
        var specifiedDate = new Date(e.date);
        if (specifiedDate.getMinutes() == 0)
        {
            specifiedDate.setMinutes(1);
            $(this).data('DateTimePicker').date(specifiedDate);
        }
    });

This also work for setting a default hour or both. Note that this code sample works with the useCurrent: false parameter too.

When the user pick a date, we check if the minute is 0 (hard-coded default value). In this case, we set a 1. Then, the user cannot select 0.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-14 16:35

I solved my problem like this

$('#startdatetime-from').datetimepicker({
language: 'en',
format: 'yyyy-MM-dd hh:mm',
defaultDate:new Date()
});
查看更多
forever°为你锁心
5楼-- · 2019-01-14 16:38

For use datetime from input value, just set option useCurrent to false, and set in value the date

$('#datetimepicker1').datetimepicker({
  useCurrent: false,
  format: 'DD.MM.YYYY H:mm'
});

查看更多
戒情不戒烟
6楼-- · 2019-01-14 16:41

It works for me:

 <script type="text/javascript">
        $(function () {
            var dateNow = new Date();
            $('#calendario').datetimepicker({
                locale: 'es',
                format: 'DD/MM/YYYY',
                defaultDate:moment(dateNow).hours(0).minutes(0).seconds(0).milliseconds(0)                    
            });
        });            
    </script>
查看更多
虎瘦雄心在
7楼-- · 2019-01-14 16:41

use this after initialisation

$('.form_datetime').datetimepicker('update', new Date());
查看更多
登录 后发表回答