Search code examples
twitter-bootstrapdatepickerjquery-multidatespicker

bootstrap datepicker multidates getDates


I am using bootstzrap datepicker with multidates option on

when I use

.datepicker("getDates")

i get date in format "2014-10-13T22:00:00.000Z" which results in error when trying to insert this values into the same multidatepicker on page reload, since there I use format dd.mm.yyyy

the funciton i use to insert dates is

.datepicker("setDates",nbworkersDates[i][0]) 

where nbworkersDates[i][0] is an array of elements

the error I am getting is that all dates are set to today any suggestions how to solve the problem? thank you

this is my whole code to fill the row with data

for (var i in nbworkersDates) {
    var innerdata = (nbworkersDates[i]);
    $('.workerDatesrow').each(function(i,r){
        //console.log(nbworkersDates[i][0]);
        //$('.nbworkersDate',r).val(nbworkersDates[i][0]),
        $('.nbworkersDate',r).datepicker({
            multidate: true,
            format: 'dd.mm.yyyy',
            multidateSeparator: ','
                        }).datepicker('setUTCDates', nbworkersDates[i][0].map(function(x) {
                            console.log('datum '+ x);   
                            return new Date(x.substr(0, 10));
    }));
        $('.nbworkersDate', r).datepicker("setDates",nbworkersDates[i][0]),
        $('.nbworkersnumber',r).val(nbworkersDates[i][1]),
        $('.nbworkersfrom',r).val(nbworkersDates[i][2]),
        $('.nbworkersto',r).val(nbworkersDates[i][3]);
        }); 

and data in the json

 "nbworkersDates":[[["2014-10-13T22:00:00.000Z","2014-10-14T22:00:00.000Z","2014-10-15T22:00:00.000Z"],"","",""],[["2014-10-13T22:00:00.000Z","2014-10-20T22:00:00.000Z","2014-10-27T23:00:00.000Z"],"","",""]]

Solution

  • The problem is that bootstrap-datepicker internal method parseDate cannot parse your date strings. See at line 1541. The condition parts.length === fparts.length is not satisfied for your strings. For example, '2014-10-14T00:00:00.000Z' will be splited in parts = ['2014', '10', '14T00', '00', '00', '000Z'], when format = 'yyyy-mm-ddT00:00:00.000Z' will be splited in fparts = ['yyyy', 'mm', 'dd']. Then the condition will be 6 === 3 and parseDate will return just current date.

    Create new Date object before 'setDates' (use 'setUTCDates' instead), a time part should be zero. (example)

    $('#date').datepicker({
        multidate: true,
        format: 'yyyy-mm-ddT22:12:34.000Z',
        multidateSeparator: ',   '
    }).datepicker('setUTCDates', [
        '2014-10-14T22:00:00.000Z',
        '2014-10-15T22:14:00.000Z',
        '2014-10-16T22:20:00.798Z'
    ].map(function(x) {
        return new Date(x.substr(0, 10));
    }));