ss888
ss888

Reputation: 1748

Jquery put dates into array

I'm querying a google calendar using json, then posting the resulting data to a php file using ajax, which formats the dates, then returns them as the following string of arrays:

["2014-03-06"]["2014-05-01","2014-05-02","2014-05-03","2014-05-04","2014-05-05"]["2014-02-21"]

I then need to get this string of arrays and somehow make it into this:

var arDates = [new Date("2014-02-21").valueOf(),new Date("2014-03-06").valueOf(),new Date("2014-05-01").valueOf(),new Date("2014-05-02").valueOf(),new Date("2014-05-03").valueOf(),new Date("2014-05-04").valueOf(),new Date("2014-05-05").valueOf()];

... so that I can disable some dates in a jquery datepicker.

This question follows on from a previous question i posted - jQuery PickMeUp datepicker: disable array of dates

Any ideas how I do this please? Full code below: (including a few things i've already tried unsuccessfully.

        $j=jQuery.noConflict(); 
        $j(document).ready(function(){                      
            var eventName = '';
            var gclaData = 'http://www.google.com/calendar/feeds/hello%40freestretch.co.uk/public/basic?orderby=starttime&sortorder=ascending&max-results=8&futureevents=true&alt=json';
            $j.getJSON(gclaData,function(data){
                for(var i = 0; i < data.feed.entry.length; i++){
                    eventName += data.feed.entry[i].summary.$t+"</br>";
                }                                   
                $j.ajax({
                    type: 'POST',
                    url: 'myprocess.php',
                    data: {'dates': eventName},
                    success: function(gcdates) {
                        // returned data is: ["2014-03-06"]["2014-05-01","2014-05-02","2014-05-03","2014-05-04","2014-05-05"]["2014-02-21"]
                        var str = gcdates.replace(/]/g, ",");
                        str = str.replace(/\[/g, "");
                        var lastChar = str.slice(-1);
                        if(lastChar == ',') {
                            str = str.slice(0, -1);
                        }                                   
                        //var match = str.split(',');
                        //console.log(str)
                        /*for (var a in match){
                            var mynewdate = match[a];
                            //mynewdate = 'new Date(' + mynewdate + ').valueOf()';
                            //console.log(mynewdate)
                        }   */
                        //console.log(str);                     
                        var arDates = [new Date("2014-02-21").valueOf(),new Date("2014-03-06").valueOf(),new Date("2014-05-01").valueOf(),new Date("2014-05-02").valueOf(),new Date("2014-05-03").valueOf(),new Date("2014-05-04").valueOf(),new Date("2014-05-05").valueOf()];
                        //console.log(arDates)                                              
                        $j('input#cdate').pickmeup({
                            position  : 'right',                                        
                            mode      : 'range',                            
                            render: function(date) {
                                return {                                    
                                    disabled: arDates.indexOf(date.valueOf()) != -1                             
                                }
                            }                           
                        });                                     
                    }
                });           
            }); 
        }); 

Upvotes: 0

Views: 164

Answers (1)

JaredPar
JaredPar

Reputation: 755587

Why is jquery needed here? A simple loop can solve this

var source = ["2014-03-06","2014-05-01","2014-05-02"];
var dest = [];
for (var i in source) {
  dest.push(new Date(i).valueOf());
}

Upvotes: 1

Related Questions