[javascript] Set default time in bootstrap-datetimepicker

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.

This question is related to javascript jquery twitter-bootstrap datetimepicker

The answer is


By using Moment.js, i able to set the default time

var defaultStartTime = moment(new Date());
var _startTimeStr = "02:00 PM"


    if (moment(_startTimeStr, 'HH:mm a').isValid()) {
        var hr = moment(_startTimeStr, 'HH:mm a').hour();
        var min = moment(_startTimeStr, 'HH:mm a').minutes();

        defaultStartTime = defaultStartTime.hours(hr).minutes(min);
    }

$('#StartTime').datetimepicker({
    pickDate: false,
    defaultDate: defaultStartTime
});

Hello developers,

Please try this code

var default_date=  new Date(); // or your date
$('#datetimepicker').datetimepicker({
    format: 'DD/MM/YYYY HH:mm', // format you want to show on datetimepicker
    useCurrent:false, // default this is set to true
    defaultDate: default_date
});

if you want to set default date then please set useCurrent to false otherwise setDate or defaultDate like methods will not work.


use this after initialisation

$('.form_datetime').datetimepicker('update', new Date());

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>

This Works for me. I have to use specially date format like this 'YY-MM-dd hh:mm' or 'YYYY-MM-dd hh:mm'

$('#datetimepicker1').datetimepicker({
            format: 'YYYY-MM-dd hh:mm',
            defaultDate: new Date()
        });

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

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


None of the above worked for me, however I had success setting the default at time of instantiation.

JQuery

<script type="text/javascript">
    $(function () {
        var dateNow = new Date();
        $('#datetimepicker').datetimepicker({
            defaultDate:dateNow
        });
    });
</script>

Momentjs.com has good documentation on how to manipulate the date/time in relation to the current moment. Since Momentjs is required for the Datetimepicker, might as well use it.

var startDefault = moment().startof('day').add(1, 'minutes');
$('#startdatetime-from').datetimepicker({
    defaultDate: startDefault,
    language: 'en',
    format: 'yyyy-MM-dd hh:mm'
});

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


This works for me after a long hours of search , This solution is for Bootstrap datetimepicker version 4. when you have to set the default date in input field.

HTML

"input type='text' class="form-control" id='datetimepicker5' placeholder="Select to date" value='<?php echo $myDate?>'// $myDate is having value '2016-02-23' "

Note- If You are coding in php then you can set the value of the input datefield using php, but datetimepicker sets it back to current date when you apply datetimepicker() function to it. So in order to prevent it, use the given JS code

JAVASCRIPT

<script>
$('#datetimepicker5').datetimepicker({
           useCurrent: false //this is important as the functions sets the default date value to the current value 
           ,format: 'YYYY-MM-DD'
        });
</script>

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.


I solved my problem like this

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

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to twitter-bootstrap

Bootstrap 4: responsive sidebar menu to top navbar CSS class for pointer cursor How to install popper.js with Bootstrap 4? Change arrow colors in Bootstraps carousel Search input with an icon Bootstrap 4 bootstrap 4 responsive utilities visible / hidden xs sm lg not working bootstrap.min.js:6 Uncaught Error: Bootstrap dropdown require Popper.js Bootstrap 4 - Inline List? Bootstrap 4, how to make a col have a height of 100%? Bootstrap 4: Multilevel Dropdown Inside Navigation

Examples related to datetimepicker

Angular-Material DateTime Picker Component? DateTimePicker time picker in 24 hour but displaying in 12hr? Set default format of datetimepicker as dd-MM-yyyy Set default time in bootstrap-datetimepicker Get the value of bootstrap Datetimepicker in JavaScript Bootstrap date and time picker datetimepicker is not a function jquery How to change the date format of a DateTimePicker in vb.net jQuery date/time picker How to get only the date value from a Windows Forms DateTimePicker control?