DateTimePicker date format

0 votes
asked Apr 26, 2016 by stan

I am trying to initialize a datetimepicker with the following :

$("#dtp").datetimepicker({
    format: 'DD/MM/YYYY HH:mm', 
    locale: 'fr', 
    defaultDate: new Date('26/04/2016 00:00'), 
    keepOpen: true
});

I get this error on Chrome browser :

bootstrap-datetimepicker.min.js:10 Uncaught TypeError: defaultDate() Could not parse date parameter: 26/04/2016 00:00

It is working for Firefox and IE.

2 Answers

+1 vote
answered Apr 26, 2016 by anton

According to MDN - parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.

I suggest to use this style

defaultDate: new Date(2016 , 4, 26, 0, 0, 0),

As for string representation it should be in RFC2822 or ISO 8601 format according to JS documentation. For example

defaultDate: new Date('2016-04-26T00:00:00')
+2 votes
answered Apr 26, 2016 by vicky-r

A JavaScript Date creates a instance that represents a single moment in time. Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC.

Syntax:

new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);

dateString-

String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).

In your case, the date string '26/04/2016 00:00' is not recognized a standard format in Chrome JavaScript engine.

For more reference see the link - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date

Hope this will help to solve your issue !!

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...