0

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
  • @Jin Yong: Was there a special reason that made you delete your post? It's a valid question after all... (@Jeff Atwood: Thanks for undeleting.) Commented Apr 17, 2009 at 10:01

3 Answers 3

2

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]
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

try

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

but this solution is locale dependent

2 Comments

new Date("2009/12/12").toString().split('/'); // ["Sat Dec 12 2009 00:00:00 GMT-0500 (Eastern Standard Time)"]
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'.

Your Answer

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.