0

I'm sure this is a simple question, but I couldn't find the answer.

I've included the code as well as my explanation.

var dates = "'2012-07-22','2012-07-26','2012-07-28'";

i have this JavaScript variable and i need to pass it the below filterDates array...something like this

filterDates = [dates]; 

or

filterDates = [document.write(dates)];
var filterDates = [];

I really don't know how to do it with JavaScript...for example we can do it in php like this

filterDates = [<?php echo $thatVariable; ?>];

How do I do this in JavaScript?

Alastair Pitts
19.7k9 gold badges72 silver badges98 bronze badges
asked Jul 18, 2012 at 6:43
2
  • 1
    var filterDates = ['2012年07月22日','2012年07月26日','2012年07月28日'] is perfectly acceptable too by the way (not sure if you created dates just for this purpose) Commented Jul 18, 2012 at 6:48
  • actually i am getting dynamic dates with ajax get method..then passing the resposne data in a global javscript variable and now want to pass that variable in filterDates.... Commented Jul 18, 2012 at 6:56

2 Answers 2

5

Use split

filterDates = dates.split(',')

That will give you the following array:

filterDates = ["'2012-07-22'","'2012-07-26'","'2012-07-28'"]

If you want them to be actual dates, rather than string representations of dates, you'll have to do some more processing, but I'm not sure that's what you're after at all?

filterDates.forEach(function(d, i) { 
 filterDates[i] = new Date(d.replace(/\'/g, ''));
});
answered Jul 18, 2012 at 6:45
Sign up to request clarification or add additional context in comments.

1 Comment

i am trying to filter dates to unvailableDates in jquery date-picker....thanks...it's not printing them into filterDates array...
3
var filterDates = dates.split(',');
$.each(filterDates,function(i,val){
 filterDates[i] = val.split("'")[1];
})
answered Jul 18, 2012 at 6:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.