views:

27

answers:

2

Hi my data is dat1; the split statement is

var splitstr =  dat1.split("-");

I have splited the data from this format

2010 -02-02 to Element 0 = 2010
Element 1 = 05
Element 2 = 22

this format..Using split function,

i want to arrange that like 2010,02,02 how can i do that

A: 

You could simply replace - with ,:

dat1.replace('-', ',');

That will give you: 2010,02,02

Sarfraz
@sarfraz Thanks it is working for me
udaya
This will only replace the first instance of '-' in the string.
Justin Johnson
@udaya: You are welcome........ :)
Sarfraz
A: 

You can either alter the original string

var date = "2010-02-02";
var formatted_date = date.replace(/-/g, ',');

Note that the g flag must be used to replace all instances of '-'.

Or, you can create a new string from the array that you've created from your original split

var split_date = date.split('-');
...
var formatted_date = split_date.join(',');
Justin Johnson
What's the downvote for?
Justin Johnson