1

I'm using https://github.com/Eonasdan/bootstrap-datetimepicker widget to show calendar. But i don't understand how to set initial value from server via js.

I receive value from server in format:

Fri, 01 Jan 2016 00:00:00 GMT

But all my attempts to set default date failed:

$el.datetimepicker({
    format: 'YYYY-MM-DD HH:mm:ss',
    defaultDate: moment(this.value, "YYYY-MM-DD HH:mm:ss")
})

or

$el.datetimepicker({
    format: 'YYYY-MM-DD HH:mm:ss',
    defaultDate: new Date(this.value)
})

Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.

or

$el.datetimepicker({
    format: 'YYYY-MM-DD HH:mm:ss',
    defaultDate: moment(new Date(this.value), "YYYY-MM-DD HH:mm:ss")
})

TypeError: defaultDate() Could not parse date parameter: 1029452400000

Arti
  • 5,595
  • 11
  • 44
  • 102

2 Answers2

2

Since there is a TypeError of the third block, you can try to cover the this.value to a "yyyy-mm-dd hh:mm:ss" format by yourself.

This question may help you to deal with it:

Format JavaScript Date to yyyy-mm-dd

best wishes.

Community
  • 1
  • 1
Simon Bai
  • 118
  • 1
  • 10
1

Not sure which server side language you are using so assuming it's PHP

First of all the server-side date format is different then then datetimepicker format, you need to change server-side date format to bootstrap datetimepicker format format: 'YYYY-MM-DD HH:mm:ss';

IF PHP and date is in Fri, 01 Jan 2016 00:00:00 GMT Format,

Change it

<?php
$sdate = "Fri, 01 Jan 2016 00:00:00 GMT";
$newdate = gmdate("Y-m-d H:m:s", strtotime($sdate));
echo $newdate;
//output will be 2016-01-01 00:01:00
?>

Now in JS, create a variable and load the newdate and set defaultDate

var serdate = '2016-01-01 00:01:00';
$('#datetimepicker1').datetimepicker({
  format: 'YYYY-MM-DD HH:mm:ss',
  defaultDate: serdate,
  allowInputToggle: true
});

Fiddle Example

Shehary
  • 9,548
  • 9
  • 33
  • 66