Hi,

I have a variable time whose value is:

time = 2010-09-17 20:00:00

I want to split this value using jQuery or java script.

After splitting
year=2010
month=09
date=17
hr=20
min=00
sec=00

Thanks.

Techie,

There's a couple of ways to do this. Here's one, not involving the Date() constructor:

function dateTime_split(time){
	var d = time.split(' ')[0];
	var t = time.split(' ')[1];
	return {
		year  : d.split('-')[0],
		month : d.split('-')[1],
		date  : d.split('-')[2],
		hr  : t.split(':')[0],
		min : t.split(':')[1],
		sec : t.split(':')[2]
	}
}

Call as follows:

time = '2010-09-17 20:00:00';
var timeObj = dateTime_split(time);

Now you can get the individual components with timeObj.year , timeObj.month , timeObj.date etc.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.