Search code examples
javascriptjquerydatetimepickerdatetime-formateonasdan-datetimepicker

Format LT datetimepicker : always returns today's date


I've to use the bootstrap datetimepicker for a legacy project.

With "LT" format, I've an issue, even if I set a default date, it returns the current date. (today).

check it on codepen

$(function () {
  $('#datetimepicker3').datetimepicker( {
    defaultDate: moment("15/05/1992","DD/MM/YYYY"),
    format: 'LT'
  });

  $('#datetimepicker3').on('dp.change',function(){
    console.log('-');
    console.log($('#datetimepicker3').data('DateTimePicker').date());
    console.log($('#datetimepicker3').data('DateTimePicker').date().format('DD/MM/YYYY HH:mm'));
    console.log('-');
  });
});

I need to receive the date I set by default in datetimepicker.

Thanks.


Solution

  • I think the problem is exactly because you are using 'LT'. LT is a format of TIME-ONLY, so the information about the date is lost. That way, when it transform the "only time" information to a date or moment object, it uses the current date.

    The defaultDate is to set the initial date/time for the component and not to define the date that will be used when dealing with time-only information.

    My suggestion: take the value from the component (as you did), extract the time and then set the desired date for that time.

    I hope it helps.