views:

97

answers:

3

I tried to split data as below, but the error "dat.split is not a function" displays. Anyone know how can I solve this problem?

var dat = new Date("2009/12/12");
var r = dat.split('/');
+1  A: 

try

dat.toString().split('/');

but this solution is locale dependent

oykuo
`new Date("2009/12/12").toString().split('/'); // ["Sat Dec 12 2009 00:00:00 GMT-0500 (Eastern Standard Time)"]`
Crescent Fresh
hey did I just found a bug in stackoverflow, I am pretty sure I deleted my answer because I found there's a problem as noted by the comment above. Plus, the author of the comment above is 'null'.
oykuo
+2  A: 

You can't split() a Date - you can split() a String, though:

var dat = "2009/12/12"; 
var r = dat.split('/');

returns:

["2009", "12", "12"]

To do the equivalent with a date, use something like this:

var dat = new Date();
var r = [dat.getFullYear(), dat.getMonth() + 1, dat.getDate()];

returns:

[2009, 4, 17]
Tomalak
+1  A: 

Do you just want to get the year, month and day? In that case you'd be better off using a non-locale dependent solution and calling the following functions:

dat.getDay();
dat.getMonth();
dat.getFullYear();

Sure they won't be zero padded, but that's easy enough to do.

Whitey