Question

I am currently stuck with the javascript split

i have the date like this 02/02/2014 - 14:57

while split

var date = s.split('/');
    var timee = s.split('-');
    var hm = timee[1].split(':');
    alert(date[2]);return false; //2014 - 14:57
 return new Date(date[2], date[1], date[0], hm[0], hm[1]).getTime();

i just want alert(date[2]);return false; //2014 remove all other strings followed by (-) hyphen

Thanks

Was it helpful?

Solution 2

Why not just change your ordering and use the result from timee?

var timee = s.split('-'),
    date = timee[0].split('/'),
    hm = timee[1].split(':');

alert(date[2]);return false; //2014 - 14:57
return new Date(date[2], date[1], date[0], hm[0], hm[1]).getTime();

OTHER TIPS

Fiddling around with splits can be tedious in the long run, regular expressions do the same in one line:

s = "02/02/2014 - 14:57"
m = s.match(/(\d{2})\/(\d{2})\/(\d{4})\s+-\s+(\d{2}):(\d{2})/)
d = new Date(m[3], m[2], m[1], m[4], m[5])
//Sun Mar 02 2014 14:57:00 GMT+0100 (CET)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top